Files and directories
Get a choking force grip on file management from the text-only CLI.
Another CLI skill that will help you manage your distro more effectively is the ability to manipulate the filesystem from the terminal. Before we get to the commands, first know that under Linux, everything (including programs, directories and devices) is a file and every file must be owned by a user. Users can only access what they own or have been given permission to run. Permission is granted because the user either belongs to the file’s group or because the file is accessible to all users. The one exception to this rule is the root user who is allowed to access all files and programs in the system.
Many times you’ll need to change the permissions of a file to make it accessible or inaccessible. The chmod command helps you change the permissions or mode of a file. Be aware that only the file’s owner, or the superuser, can change the mode of a file. The command chmod supports two distinct ways of specifying mode changes: numerical number representation or symbolic representation. Symbolic notation offers the advantage of allowing you to set a single attribute without disturbing any of the others and is divided into three parts. The first part denotes whom the change will affect and is marked with a combination of the characters u,g, o, and a: u is for user and refers to the owner of the file or directory, g is for group owner. o is short for others. a is short for all or a combination of all the characters and is the default value if no character is specified:
The second part of a symbolic notation marks which operation will be performed: Plus (‘+’) Indicates that a permission is to be added. Minus (‘-’) Indicates that a permission is to be taken away. Equals (=) Indicates that only the specified permissions are to be applied and that all others are to be removed. The third part denotes what permission will be set and is specified with the earlier mentioned r, w, and x characters.
Putting this into practice a u+x adds execute permission for the owner, while u-x removes execute permission from the owner. Similarly, go=rw sets the group owner and anyone besides the owner to have read and write permission. Any previous execute permissions given to users besides the owner are removed.
While it’s easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. For example, it’ll be a time- consuming process to copy all PDFs from one folder to another except for the ones that already exist in the destination folder. In the terminal however cp -u *.pdf destination-folder will do this without any fuss.
Manipulate files
The Linux shell provides special characters called wildcards that help specify a bunch of filenames. Two of the most commonly used wildcards are * that matches any characters and ? for matching a single character, e.g. d*.pdf matches all files that begins with the character d and ends with .pdf while d???.pdf limits itself to PDF files that begin with the character d but are followed by exactly three more characters, such as d001.pdf and d002.pdf.
Equipped with this knowledge we can now comprehend the cp command. The -u option only copies files that either don’t exist, or are newer than the existing corresponding files, in the destination directory. You can also use the -r option to recursively copy directories and their contents, such as cp -r Documents/ /shared/All_files/ .
Similar to cp is the mv command that’s used to move files from the source to the destination. If the destination file exists, it’s overwritten by the source file. Then there is the rm command that’s used to delete files and directories following the command, such as rm file1.txt and rm -r documents . However, be careful when using rm since it zaps the files straightaway instead of moving them to a recycle bin.