Working with Terminal/Shell |
|
Basic File Manipulation commands |
|
1. chmod Used for:
The chmod command allows you to alter access rights to files and directories. All files and directories have security permissions that grant the user particular groups’ or all other users’ access.
Description:
To view your files' settings, at the shell prompt type: ls -alt |
|
You should see some files with the following in front of them (an example follows): |
|
Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 | Col 7 |
-rw-rw-r-- | 1 | super | super | 1026 | Oct 8 16:43 | simple.txt |
-rw-rw-r-- | 1 | super | super | 3551 | Oct 8 16:43 | ChangeAddress (2).php |
-rw-rw-r-- | 1 | super | super | 3551 | Oct 1 09:49 | ChangeAddress .php |
-rw-rw-r-- | 1 | super | super | 8944694 | Sep 28 15:58 | MadeForYou .tar.gz |
-rw-rw-r-- | 1 | super | super | 787516 | Sep 28 15:47 | Almond-rounded .zip |
-rw-rw-r-- | 1 | super | super | 5986411 | Sep 27 15:34 | apache-tom- cat-6.0.14.- tar.gz |
-rw-rw-r-- | 1 | super | super | 890880 | Sep 27 15:32 | apache-tomcat -6.0.14- deployer.tar.gz |
-rw-rw-r-- | 1 | super | super | 363811 | Sep 25 17:02 | presentationus1 _new.zip |
-rw-rw-r-- | 1 | super | super | 4208 | Sep 25 14:18 | adobe-release- i386-1.0-1.- noarch.rpm |
Drwxrwxrwx | 2 | super | super | 4096 | Sep 24 18:08 | jdk6 |
|
|
What different columns represents in the result? |
|
Col 1:
The first character in this column tells what kind of file this is. The dash represents a normal file; the d represents a directory. The remaining characters describe the file’s permissions, which we will cover in detail in the next pages. |
|
Col 2
This column tells how many hard links there are for this file. |
|
Col 3 & 4
These two columns tell you the name of the owner of the file and the group to which the owner belongs. |
|
Col 5
The size of the file, in bytes. For directories, this is the size of the directory, not the total of the directory’s contents! |
|
Col 6
This column contains the date and time at which the file was last modified. Note that you get time of day if the modification was during this year; otherwise you see the year. |
|
Col 7
The last column contains the file name. |
|
What do the letters mean in front of the files/directories mean? |
|
r indicates that it is readable (someone can view the file’s contents)
w indicates that it is writable (someone can edit the file’s contents)
x indicates that it is executable (someone can run the file, if executable)
- indicates that no permission to manipulate has been assigned. |
|
When listing your files, the first character lets you know whether you’re looking at a file or a directory. It’s not part of the security settings. The next three characters indicate Your access restrictions. The next three indicate your group's permissions, and finally other users' permissions. |
|
Use chmod followed by the permission you are changing. In very simple form this would be: |
|
chmod 755 simple.txt |
|
The example above will grant you full rights, group rights to execute and read, and all others access to execute the file simple.txt. |
|
# | Permission |
7 | full |
6 | read and write |
5 | read and execute |
4 | read only |
3 | write and execute |
2 | write only |
1 | execute only |
0 | none |
|
|
Still confused? |
|
Use the table above to define the settings for the three "users." In the command, the first number refers to your permissions, the second refers to group, and the third refers to general users. |
|
Typing the command: chmod 751 filename |
|
gives you full access, the group read and execute, and all others execute only permission. |
|
Execute ls -l command to view the content of current directory |
|
 |
notice the permissions of the file my_folder, |
|
drwxrwxr-x 2 super super 4096 Sep 24 13:05 my_folder |
|
here d indicates that my_folder is a directory, after that, 2nd 3rd and 4th characters,[ rwx]represents owner permission and here rwx means owner can read, write and search the directory, 5th, 6th and 7th [rwx] represents the group permission and rwx means group member can read, write and search the directory. Las t three characters represents the permission for others and r-x as permission means, others can read the content of directory and on directory, but they can not write to it. |
|
Now, let us the alter the permission of the directory type the following command: chmod 777 my_folder |
|
as described earlier 7 as permission string means full permission, this means we are assigning full permission to everybody. |
|
 |
|
Now, use the ls command again to see the effect of the chmod |
|
 |
|
now notice the change, now everybody has full permission to the my_folder directory. If you want to make this folder inaccessible to other users. You can do so by executing the following command: |
|
chmod 770 my_folder |
|
 |
|
now use the ls -l command to view the effect of the last chmod command: |
|
 |
|
as you can see in the results the last thee characters that belongs to the others permission are blank, that means, users other that owner and group members have no access to the folder. |
|
2. cp Used for : Used to copy file(s) from one location to another
Description :
Type cp followed by the name of an existing file and the name of the new file. Similar to DOS copy . |
|
cp file newfile
To copy a file to a different directory (without changing th e file’s name), specify the directory instead of the new filename.
Example: |
|
cp newfile test
To copy a file to a different directory and create a new file name, you need to specify a directory/a new file name. Example: |
|
cp newfile testdir/newerfile
cp newfile ../newerfile |
|
The .. represents one directory up in the hierarchy. |
|
Now, let us copy a file with cp command type the following command:
cp readthis.txt my_folder in this command readthis.txt is the file that we want to copy and my_folder is destination folder. |
|
 |
Now use the ls command to make sure, file has been copied to the destination folder |
|
 |
3. file Used for : used to retrieve the information about the file type.
Description : Type file followed by the name of an existing file in the directory.
Ex: file contact_magnager.exe
OUTPUT: MS-DOS executable (EXE) |
|
This command allows you to figure out what the file type is and how to use it. For instance the command will tell you whether it is an executable, a compressed file and which type, or something unusual. |
|
This command is simplistic, but often can allow you to determine why a file does not respond the way you expect. |
|
 |
|
4. mv
Used for : Used to move file(s) from one location to another
Description : Type mv followed by the current name of a file and the new name of the file. Similar to DOS move
Ex: mv oldfile newfile |
|
Type mv followed by the name of a file and the new directory where you'd like to place the file. Ex: mv readthis.txt my_folder |
|
This moves the file named readthis.txt to an existing directory named my_folder. Be certain you’re specifying a directory name or the mv command alters the name of the file instead of moving it. |
|
The mv command moves files and directories from one directory to another, or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name. When you move a file, all links to other files remain intact, except when you move it to a different file system. When you move a directory into an existing directory, the directory and its contents are added under the existing directory. |
|
When you use the mv command to rename a file or directory, the TargetDirectory parameter can specify either a new file name or a new directory path name. |
|
If moving the file would overwrite an existing file that does not have write-permission set and if standard input is a workstation, the mv command displays the file-permission code and reads a line from standard input. If that line begins with a y, or the locale's equivalent of a y, the mv command moves the file. If the response is anything other than a y, the mv command does nothing to that file and continues with the next specified file. |
|
You can use the mv command to move files within the same file system or between file systems. Whether you are working in one file system or across file systems, the mv command copies the file to the target and deletes the original file. The mv command preserves in the new file the time of the most recent data modification, the time of the most recent access, the user ID, the group ID, and the file mode of the original file. |
|
The mv command will modify either the source file or the destination path if the command is prematurely terminated. |
|
Attention: The mv command can overwrite many existing files unless you specify the -i flag. The -i flag prompts you to confirm before it overwrites a file. If both the -f and -i flags are specified in combination, the last flag specified takes precedence. |
|
-f |
Does not prompt you before overwriting an existing file.
|
-i |
Prompts you before moving a file or directory to an existing path name by displaying the name of the file followed by a question mark. If you answer with a line starting with y or the locale's equivalent of a y, the move continues. Any other reply prevents the move from occurring.
|
|
|
Now, let us move a file with mv command,
type the following command to move a file:
mv readthis.txt my_folder -i
-i in this command means, if a file named readthis.txt exists in the destination folder, then it will ask you if you want to overwrite the existing file. |
|
 |
|
5. rm [remove]
Used for : Used to remove a file from the file system.
Description : |
|
The rm command removes the entries for the specified File parameter from a directory. If an entry is the last link to a file, the file is then deleted. You do not need read or write permission for the file you want to remove. However, you must have write permission for the directory containing the file. |
|
If either of the files . (dot) or .. (dot, dot) are specified as the base name portion of the File parameter, the rm command writes a diagnostic message to standard error and does nothing more with such parameters. |
|
The rm command writes a prompt to standard error and reads a line from standard input if the -f flag is not specified, and either the File parameter does not have write permission and the standard input is a workstation, or the -i flag is specified. If the response is not affirmative, the rm command does nothing more with the current file and proceeds to the next file. |
|
The files owned by other users cannot be removed if the sticky bit of the directory is set and the directory is not owned by the user. |
|
Note: The rm command supports the -- (dash, dash) parameter as a delimiter that indicates the end of the flags. |
|
Flags |
|
-e | Displays a message after each file is deleted. |
-f | Does not prompt before removing a write-protected file. Does not display an error message or return error status if a specified file does not exist. If both the -f and -iflags are specified, the last one specified takes affect. |
-i | Prompts you before deleting each file. When you use the -i and -r flags together, therm command also prompts before deleting directories. If both the -i and -f flags are specified, the last one specified takes affect. |
-r | Permits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -R flag. |
-R | Permits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -r flag. |
|
|
Type rm followed by the name of a file to remove the file. |
|
Ex: rm readthis.txt |
|
Use the wildcard character to remove several files at once. Ex: rm r* |
|
This command removes all files beginning with n. Type rm -i followed by a filename if you’d like to be prompted before the file is actually removed. |
|
Ex: rm -i readthis.txt rm -i r* |
|
By using this option, you have a chance to verify the removal of each file. The -i option is very handy when removing a number of files using the wildcard character *. |
|
|
|
|
Working with Terminal/Shell |
|
Basic Directory commands |
|
1. cd Used for : Use cd to change directories.
Description : Type cd followed by the name of a directory to access that directory. Keep in mind that you are always in a directory and can navigate to directories hierarchically above or below. |
|
Example: |
|
cd download
If the directory games is not located hierarchically below the current directory, then the complete path must be written out. |
|
Ex:
cd /home/super/downlaods |
|
 |
|
To move up one directory, use the shortcut command. |
|
Example: cd .. |
|
 |
|
2. mkdir Used for : Used to create directories on disk. Similar to DOS MD
Description : Use mkdir to make/create a brand new directory
Type mkdir followed by the name of a directory.
Ex: mkdir testdir |
|
 |
|
Create one or more directories. You must have write permission in the parent directory in order to create a directory. The default mode of the new directory is 0777, modified by the system or user's umask. |
|
Options -m mode, --mode mode |
|
Set the access mode for new directories. See chmod for an explanation of acceptable formats for mode. |
|
-p, --parents |
|
Create intervening parent directories if they don't exist. |
|
-v, --verbose |
|
Print a message for each directory created. |
|
--help |
|
Print help message and then exit. |
|
&--version |
|
Print version number and then exit. |
|
-Z context, --context=context |
|
Set security context in SELinux. |
|
Examples |
|
Create a read-only directory named personal: |
|
mkdir -m 444 personal |
|
 |
|
3. rmdir Used for :Used rmdir to remove an existing directory (assuming you have permissions set to allow this).
Description : Type rmdir followed by a directory's name to remove it.
Ex: rmdir personal |
|
 |
|
You CAN'T remove a directory that contains files with this command. A more useful command is rm -r that removes directories and files within the directories. |
|
The rmdir command is used mostly to remove empty directories. If you have a desire to use this command then you'll need to delete or move the files before attempting to remove a full directory. |
|
Options |
|
--help |
|
Print a help message and then exit. |
|
--ignore-fail-on-non-empty |
|
Ignore failure to remove directories that are not empty. |
|
-p, --parents |
|
Remove directories and any intervening parent directories that become empty as a result. Useful for removing subdirectory trees. |
|
--verbose |
|
Verbose mode; print message for each directory as it is processed. |
|
--version |
|
Print version information and then exit. |
|
|
|
|
Working with Terminal/Shell |
|
Using Vi/Vim editor |
|
Vim, which stands for Vi IMproved, is an open source, multiplatform text editor extended from vi. It was first released by Bram Moolenaar in 1991. Since then, numerous features have been added to Vim, many of which are helpful in editing program source code. Vim and vi are very popular editors for programmers and users of Unix-like operating systems. |
|
What is vi? |
|
• vim is efficient and elegant. It does things quickly by the best way.
• vim is healthful. It doesn't make your fingers and arms painful.
• vim is small and fast. It fits in one or two floppies and starts instantly.
• vim is powerful. No feature useful for text editing is missing.
• vim is simple and clean. It doesn't offer to do things irrelevant to text editing.
• vim is running everywhere. If you're willing to use it, you can, no matter what computers and operating systems you use.
• vim is free. |
|
The Unix editor vi has a long history, and is the one that vim is based on. I believe that the power, beauty and essence of vim always come from vi. A real vim user doesn't mind using vi temporarily, but feels great pain if forced a thing like Emacs. vim retains all the powers of vi, plus a lot of improvements and new features. And now vim is running virtually all platforms. So I see no reason to choose vi over vim |
|
Starting vi/vim |
|
Type vi or vim to start vi editor on prompt to start the vi editor: |
|
 |
|
or you can specify the file name at the command line. i.e. |
|
 |
press enter after entering the command. |
|
 |
this UI of vi/vim editor. |
|
Working with vi editor |
|
 |
|
In the vi editor press i/ESC to toggle between insert/command mode.. |
|
Useful vi editor commands: |
|
Move around: Use the cursor keys, or "h" to go left, "j" to go down, "k" to go up, "l" to go right.
Close this window: Use ":q<Enter>".
Get out of Vim: Use ":qa!<Enter>" (careful, all changes are lost!).
Save to file: “:w<Enter>” |
|
 |
Press Esc to toggle between command mode and insert mode. |
|
Getting started with vim editor in simple steps: |
|
1. Starting vim |
|
to start vim and edit a file (new or existing) vim hello.cpp |
|
 |
• to start vim and open a new file |
|
vim |
|
You can give the stuff you typed in a name when later you save it as a file. |
|
2. Enter text/command |
|
If this is one of the first few times you use vim, perhaps you're wondering why you cannot type in anything. This is because you're in command mode. In command mode, your key strokes are interpreted as editing commands. To begin typing new text into the file, you need to switch to insert mode by press i. Then you can see each character you type appears on the screen. |
|
The two basic modes of vim are: |
|
* Insert mode, in which anything you type (except some special keys) will appear on the screen and become part of your file buffer.
* Command mode, in which your key strokes are interpreted as commands. |
|
After you start vim, you're in command mode. |
|
How to Switch between modes |
|
1. From command mode to insert mode, press |
|
 |
|
2. from insert mode to command mode, press ESC |
|
3. How to Navigate ? |
|
In both the command mode and the insert mode you can always use the arrow keys to move the cursor, and with gvim you can click mouse to get to new position. However, these are not the ways of vi guys. If you do that, you're not using vim, you're using notepad. |
|
If you want to call yourself a vi guy, forget about the arrow keys, backspace, delete, insert, ... and of course, the mouse. The most effective ways of moving cursor is first go to the command mode by pressing ESC and then use vim's cursor-moving commands to move around. |
|
The 4 basic commands are: |
|
• j move down one line
• k move up one line
• h move left one character
• l move right one character |
|
Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys. |
|
4. Copy, Cut and Paste |
|
How to COPY, CUT and PASTE |
|
If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse. |
|
1. Enter the command mode by pressing ESC
2. Move the cursor to the line which you want to make a copy, by pressing j or k
3. press
yy to make a copy of the line, or dd to cut it and make a copy |
|
 |
|
4. now move cursor (by pressing k or j) to the the location where you want to put this copy
5. press
p
to put the buffer after current line, or
P
to put the buffer before current line |
|
|
 |
|
If you want to copy or cut several lines, put a number before the yy or dd command, like |
|
2yy |
|
to copy 2 lines. |
|
5. Searching |
|
Suppose you want to find all the words apple in your file |
|
1. Make sure you are in command mode by hitting ESC
2. type
/eBIZ.com |
|
followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted. |
|
 |
|
3.
4. after you got the first eBIZ.com, you can keep typing
n
to find other eBIZ.com |
|
6. Exisiting vim |
|
• save your file before you exit: press ESC, then type :
wq
or
ZZ |
|
 |
|
• save your file as newname before you exit: press ESC, then type :
wq newname |
|
• Exit without saving, press ESC, then type :
q |
|
• Forced quit
If :q doesn't work, it's probably because you didn't save the change. If you want to save, use :wq. If you don't want to save the changes, type :
q! |
|
 |
|
|
|
|
|
|
|
|
No comments:
Post a Comment