Mv
The mv
command is used to rename a file. The syntax looks like: mv SOURCE_FILE DESTINATION_FILE
. For example, to rename a file called failed.txt to passed.txt, you would enter the following command: mv failed.txt passed.txt
Cp
The cp
command is used to copy files or directories.
The syntax is: cp SOURCE DESTINATION
For example, to duplicate the passed.txt to a file called passed2.txt you would enter: cp passed.txt passed2.txt
Rm
The rm
command deletes (removes) files or directories.
The syntax is: rm FILENAME
For example to delete the file called failed.txt, the command would be: rm failed.txt
By default, the rm
command does not delete directories if there are contents within the directory. The directory either needs to be empty or the -r switch must be used.
Chmod
The chmod
command changes the mode of a file or object. The different modes are read (r), write (w), and execute (x).
In Linux, you can set modes for the file owner (u), a group (g), others (o), or all (a).
For a file, you would have mode that looks something like -rwxr- -r- -. The first rwx indicates that a user has read, write, and execute permissions. The next r- – indicates that a group has only read permission. The last r- – says that others also have a read only permission.
The read, write, and execute permissions also have an associated number, based on binary. For example if read, write, and execute are turned on in binary that is 111 which is equal to 7. Read only means that is on, write is off, and execute is off which is 100 in binary, which is equal to 4.
Number | Permission | r w x |
7 | Read, write, execute | r w x |
6 | Read and write | r w – |
5 | Read and execute | r – x |
4 | Read only | r – – |
3 | Write and execute | – w x |
2 | Write only | – w – |
1 | Execute only | – – x |
0 | None | – – – |
Therefore our file with -rwxr- -r- – is equivalent to 744.
The syntax for chmod is: chmod mode FILE
So for example, let’s say we have a file called failed.txt with a mode of -rwxr- -r- – (or 744). We want to change the permission so that the group permission is able to read and write to the file. The command would be: chmod 764 failed.txt
The chmod command allows for some shortcuts. For example chmod a-w failed.txt
would change the mode so that all users have no writing permissions for failed.txt. Another shortcut example is chmod u+x passed.exe
, which would mean the owner (u) of failed.ext can execute (x) the passed.exe file.
The ls-l will list the permissions of objects.
Chown
The chown
command is used to change the file owner and group for objects.
The syntax is: sudo chown [OWNER:GROUP] FILE
For example, to change the owner of file passed.txt from me to Bob, the command would look like: sudo chown Bob passed.txt