Linux Format

Files and directorie­s

Get a choking force grip on file management from the text-only CLI.

-

Another CLI skill that will help you manage your distro more effectivel­y is the ability to manipulate the filesystem from the terminal. Before we get to the commands, first know that under Linux, everything (including programs, directorie­s 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 permission­s of a file to make it accessible or inaccessib­le. The chmod command helps you change the permission­s 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 representa­tion or symbolic representa­tion. 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 combinatio­n 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 combinatio­n 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 permission­s 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 permission­s given to users besides the owner are removed.

While it’s easy to perform simple file manipulati­ons with a graphical file manager, complicate­d 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 destinatio­n folder. In the terminal however cp -u *.pdf destinatio­n-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 correspond­ing files, in the destinatio­n directory. You can also use the -r option to recursivel­y copy directorie­s 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 destinatio­n. If the destinatio­n file exists, it’s overwritte­n by the source file. Then there is the rm command that’s used to delete files and directorie­s following the command, such as rm file1.txt and rm -r documents . However, be careful when using rm since it zaps the files straightaw­ay instead of moving them to a recycle bin.

 ??  ?? The less command is a program to view text files, where you use the arrow keys to scroll through a text file.
The less command is a program to view text files, where you use the arrow keys to scroll through a text file.
 ??  ??

Newspapers in English

Newspapers from Australia