270 likes | 471 Views
Basics of the Unix/Linux Environment. File Permissions and Text Editing. Dealing with file names with special characters.
E N D
Basics of the Unix/Linux Environment File Permissions and Text Editing
Dealing with file names with special characters • Say I have a file named “!”. (this is probably because I used >! at some time while in bash, but this syntax is for tcsh not bash, so I redirected my output to a file called !) %ls ! ! %rm ! remove !? Y
What about a file named “-”? Make a file named “-” with touch command (use man to see what the touch command does) %touch - %ls - f2.dat HW Try to remove it. %rm - usage: rm [-fiRr] file ... What is the problem? (you tell me.)
We have to let the shell know that the “-” is NOT a switch. Use the “-” switch all by itself. %rm - - rm: remove - (yes/no)? y alpaca.ceri.memphis.edu193:>
Remember that filenames can have any character but the “/” (used to define the path), so sooner or later you are going to get a file name that will be hard or dangerous to reference. You will have to be especially careful/creative if you get a file named “*” as %rm * is disastrous (and the more privileges you have and the higher up you are in the directory structure, the more disastrous it is.)
Ownership • User • the person who created or is in charge of the file/directory (u) • Group • a group of users that can be associated with the file/directory (g) • Others • any user not part of the User or Group designation (o)
Permissions • Read • ability to read the file/directory (r) • Write • ability to write or overwrite the file/directory (w) • Execute • ability to execute or run the file and view directories (x) • if a directory is not executable, you cannot cd into it or see what is in it at all.
Viewing ownership & permissions • ls -l: lists long format % ls -aFl *pl bin | head -n 10 -rw-r--r-- 1 hdeshon user 42 Aug 20 13:47 helloworld.plstandard permissions -rwxr-xr-x 1 hdeshon user 1276 Aug 7 2007 res2sacfiles.pl* preferred executable permissions bin: used to store executable scripts and programs for global access total 40722 drwxr-xr-x 3 hdeshon user 3584 Aug 1 13:50 ./ standard directory permissions drwxr-xr-x 41 hdeshon user 2048 Aug 21 10:07 ../ -rwxr-xr-x 1 hdeshon user 13508 Aug 1 13:50 addcglags* -rwxr-xr-x 1 hdeshon user 1155 Aug 1 13:50 addnoise* -rwxr-xr-x 1 hdeshon user 49152 Aug 1 13:50 ah2asc*
Changing owners and groups • If you create a file, you are the owner/user. • Mitch and Bob have the system set up to automatically set group to ‘user’, or all users of the CERI unix system. • Default for CERI files are rw-r--r-- otherwise known numerically as 644
Changing Permissions • chmod: change file or directory permissions %chmodugo+xhelloworld.pl %ls –lFhelloworld.pl -rwxr-xr-x 1 hdeshon user 42 Aug 20 13:47 helloworld.pl* %chmodgo-rxhelloworld.pl %ls–lFhelloworld.pl -rwx------ 1 hdeshon user 42 Aug 20 13:47 helloworld.pl* • –R flag allows you to set all files to the same permissions within a directory and all subdirectories
Changing Permissions • you can also use numbers to change ownership • 644 represents u+rw; g0=r • 755 represents u+rwx; go=rx#I use this one a lot %chmod 775 helloworld.pl %ls–lFhelloworld.pl -rwxr-xr-x 1 hdeshon user 42 Aug 20 13:47 helloworld.pl*
Text Editing Options • Mouse-driven options • nedit: this GUI text editor allows interactive mouse or keyboard driven text manipulation; colored text and auto-recognition of various standard scripting and programming languages is helpful for debugging scripts and code; appears to be a student favorite at CERI and is available on the Unix system • emacs: a less sleek looking GUI text editor allows interactive mouse or keyboard driven text manipulation; it is very powerful and is an old favorite of computer programmers
Text Editing Options • Keyboard-driven options • vim: this non-GUI text editor relies primarily on keyboard driven text manipulation; steep learning curve but very powerful; colored text and auto-recognition of various standard scripting and programming languages is helpful for debugging scripts and code; my personal favorite • pico: a pared down non-GUI text editor very similar to the email program pine. If you don’t know what pine is, use nedit instead.
OK, nedit or vim • nedit is available on the CERI unix machines because Bob and Mitch have installed it • nedit has a shallow learning curve • vim is available standard on all unix and unix-like systems • vim is harder to learn *note to OSX users, nedit can be downloaded and installed on OSX. Xcode is a similar but more powerful editor for code development.
NEdit • to start it up %nedit &an & placed at the end of a command line opens the program in the background so that you can continue to use the terminal window.
vim • to start it up %vim [name-of-file] • Two modes • normal/command mode • insert/input mode • Typing takes place in insert mode and the editing power comes to the fore in normal mode • Use esc to toggle out of insert mode
moving the cursor esc type esc to enter normal mode $ 0 ^ ^f ^ ^b ^ control key $ -- go to end of line (eol) 0 -- go to beginning of line (bol) ^ -- go to first character at bol ^f -- scroll screen forward ^b -- scroll screen backwards
to enter insert mode esc type esc to exit insert mode I,i A,a s i -- insert a -- append s -- substitute A -- append at end of line I -- insert at beginning of line
deleting text esc type esc to enter normal mode dw dd X,x x -- delete character behind cursor X -- delete character in front of cursor dw -- delete word dd -- delete line Xdd -- delete next X lines
copy, paste, undo and redo esc type esc to enter normal mode yw yy p U,u ^R control key ^ . ^ control key yy -- copy the line (yank) yw -- copy the word (yank) p -- paste the line or word after the cursor u -- undo change U -- undo all changes to the line ^R -- redo change . -- repeat last command
search and replace esc type esc to enter normal mode :s/ :g/ : return n / /[word(s)] -- search for the next instance of the word or words n -- go to next instance of word or words :s/[old]/[new] -- substitute old with new string; cursor is on old string :g/[old]/s/[old]/[new]/g -- globally find old, substitute all old with new
saving and exiting vim esc type esc to enter normal mode :q :w : return shift ZZ shift :w [filename] -- write to file :w! [filename] -- overwrite file :wq -- write and quit :q --- quit :q! --- quit without saving ZZ -- write and quit
other useful features :![unix command] -- allows you to run standard unix commands without exiting vim; very useful with GMT Example :!ls *.SAC list all sac files in the current directory :set hlsearch -- will highlight all instances of a string when using /[word] to search >aB -- indent the block/loop defined by {} when cursor is located within the block in question :sp -- split the screen ^WW -- use to move from one split screen to the next; useful when writing subroutines within the same file : set number or :set nonumber -- turn line numbers on/off :X -- jump to line number X example :1go to first line of file