File Management Getting Around

the name of the file garden.png while keeping it in the same directory, you could use mv ./garden.png ./ sun‑yat‑sen‑gardens.png. As you copy or move files, you might want to create a new directory with mkdir. Although this is a relatively straightforward , you can fine- tune it with ‑ ‑mode=octal‑permissions to set permissions for the new directory or create the directories immediately above it by adding the ‑p (parent) option. Kevin Penhallow, 123RF Learning file management commands To delete, use rm (remove) for files and directories and rmdir for directories. Don’t forget that, unlike the desktop, the Bash shell has no Trash folder. The clos- est you can get is to create a special File Power folder and move files to it instead of using rm or rmdir. By default, rm works only on files. To We give you an overview of commands for moving, editing, - delete directories with it, you have to use the ‑r option. As you might imagine, ing, and generally manipulating files. By Bruce Byfield rm ‑r can remove key system files when used thoughtlessly; thus, some users NU/​Linux treats everything as a overwritten or the ‑u option to overwrite prefer to add ‑ ‑preserve‑root when run- file. For this reason, learning file only files that are newer than the ones in ning the command anywhere near the Gmanagement commands should the target directory (Figure 1). root directory. In comparison, rmdir is a be among your first priorities in learning Also, you can add ‑ ‑preserve=mode to much safer option, because it works about the . These com- choose to preserve file attributes, such only on empty directories (Figure 4). mands are easy to remember because as owner or timestamp, or ‑ ‑no‑preserve A completely different approach to file their names are usually abbreviations of =mode to have them changed in the management is taken by dd, an old their actions – for example, mv for move files’ new location. Whether or not you utility that copies bytes or blocks rather and ls for list – but their options can take preserve attributes is especially impor- than files. Used mainly by administra- time to learn. tant when you are logged in as root and tors, dd has a non-standard syntax that Basically, file management commands moving around files owned by another is far too complex to detail here. Briefly, fall into three categories: directory and user – say, for a backup of the /home di- though, dd can be used for such tasks as file movement, navigation and editing, rectory. creating an ISO image from a CD/​DVD, and compression. Commands in all three Sometimes, you might not want to wiping a disk by filling it with random categories are typically more powerful waste hard drive space on multiple cop- data, and duplicating a partition or mas- (and potentially more dangerous) than ies of the same file, in which case you ter boot record. Just remember to con- their desktop equivalents, thanks mainly might prefer to use ln ‑s file link to create struct your dd command carefully and to to file globbing, or the use of standard a symbolic link, or pointer, to the origi- double-check it. Even more than rm, the patterns to refer to multiple files. nal file, which takes up much less space dd command can be hazardous to your (Figure 2). Later, if you copy these sym- system if you are inattentive. Moving and Editing bolic links to a backup, you can use Directories and Files ‑L to ensure that the original file, not the Navigating and Editing The most basic command for moving di- link, is used. Directories and Files rectories and files is cp. Its structure is Alternatively, you might prefer to You probably already know that you simple: cp move a file with mv, which takes many move around the directory tree with the . By default, cp overwrites any of the same options as cp. Also, you can command cd – a command files of the same name in the target di- use mv to rename a file, giving it the so simple that it has no options. You rectory, but you can be cautious and use same directory path but a different final might not know, however, that cd has the ‑b option to back up any files that are name (Figure 3). For example, to change several shortcuts: cd .. moves to the di- rectory immediately above the current one; cd ‑ returns you to the previous di- rectory; and cd ~ returns you to your Figure 1: The cp command allows you to be both cautious and flexible. Here, the root user home directory (Figure 5). Combined ensures that files with the same name as those being copied are not overwritten and that the with the command history in a virtual owner of the files does not change. terminal, these shortcuts are enough to

linux shell handbook 9

009-011_filemanagement.indd 9 02/08/11 9:51:26 PM Getting Around File Management

but it continues to be important for cre- ating backups or sending files as email Figure 2: Creating a symbolic link with ln is a space-saving way of having the same file in two attachments. The Bash shell includes places at the same time. four commands for compression: the original , , , and – more rarely – cpio. When you exchange files with users of Figure 3: The mv command does double-duty, both moving files and renaming them. other operating systems, use gzip so they can open the archive. Gzip’s basic use is straightforward, with a list of files fol- lowing the command, but you can use a variety of options to control what hap- Figure 4: The rmdir command is much safer to use than rm ‑r, because it can’t delete directo- pens. ries that still have files in them. To set the amount of compression, you can use the parameter ‑ ‑best , or to set the speed of compres- forward buttons in a web browser. the most important ones: sion, you can use ‑ ‑fastest . Once you are in a directory, use ls to • ‑amin : Minutes since a Both are measured on a scale of 1 to 9. view the contents. In many distributions, file was accessed. Note that you need to use the ‑N option you will that ls is actually an alias of • ‑cmin : Minutes since a to preserve the original files; otherwise, ls ‑ ‑color, which displays different types file was changed. they will be deleted when the archive is of files in different colors. Sometimes, it • ‑atime : Days since a file was created. is an alias of ls ‑ ‑color ‑ ‑classify, which accessed. To work with files in a gzip archive, adds the use of symbols such as / to in- • ‑amin : Days since a file was you can use several utilities: dicate a directory or * to indicate an exe- changed. • zcat displays files in a gzip archive. cutable file (Figure 6). • ‑group : Files that belong to • zcmp compares files in a gzip archive. For many users, these options are a particular user group. • zdiff lists differences between files in a more than enough. However, sooner or • ‑user : Files that belong to a gzip archive. later, you will likely need the ‑a option, particular user. • zgrep, zegrep, and zfgrep search for which displays hidden files – those • ‑maxdepth : The maxi- text patterns in gzip-archived files. whose names start with a period. To pin- mum level of sub-directories in which One especially useful utility is gunzip, point a file, you might use ‑l to display to search. which amounts to an alias for gzip be- file attributes. To help sort files with ls, • ‑mindepth : The minimum cause it uses most of the same options. various options let you sort by size (‑s), level of sub-directories in which to But, if you can’t be bothered learning time (‑t), or extension (‑X). search for newer file names; files that another command, you can simply use All this information can easily occupy are newer than the one mentioned in the command gzip ‑d. more lines than your terminal window the option. By contrast, the bzip2 command pro- displays, so you might want to pipe the • ‑perm : Designated duces archives that are 10%-20% command through less (ls | less) so that permissions. smaller than those produced by gunzip. only one screen full of information is • ‑e : Excludes files of a cer- But, although bzip2 and gzip serve simi- visible at a time. If you are trying to tain sort from the search. Common file lar purposes, bzip’s options are consider- identify a file, file is a supplement to ls, types include ascii and compress. ably different. For one thing, you have to identifying the type of file (Figure 7). If When you have located a file, you can specify sub-directories, because bzip2 you have symbolic links, you can add use the touch command to edit its time- lacks an ‑r option. For another, you use the ‑L option so that you can identify the stamps. For example, the command the ‑z option to compress files and ‑d to type of the original file. Also, you can touch ‑a grocery list.txt 0910311200.00 decompress. To keep the original files use ‑z to view the contents of com- would change the access time to noon pressed files (more on this later). on October 31, 2009, and you can use Yet another tool for tracking down files the same date format after ‑m to change is find. The find command takes so the last modification time. Similarly, ‑t=YYMMDD.ss changes the date and the time that the file was created. Also note that the time starts with the last Figure 5: The shortcuts in the change direc- two digits of the year and ends with the tory command are much faster than typing seconds. out the entire name of a directory. They Figure 6: Many distributions create an alias require one or two characters – far fewer Compressing Files for the ls command, so that it automatically than when typing the names of most direc- Compression is less essential now than it displays different types of files with differ- tories in your home. was in the days of 100MB hard drives, ent colors.

10 linux shell handbook

009-011_filemanagement.indd 10 02/08/11 9:51:27 PM File Management Getting Around

after the archive is created, use the ‑k directory tree and create archives in mul- rightly pride themselves on their ability option. tiple formats (of which TAR is the only to construct elaborate and elegant globs. Like gzip, bzip2 has some related utili- one that is widely used), as well as nu- But what if you want to work with a ties for working with its archives: merous options to view and edit already- metacharacter? Then you put a back- • bzipcat displays the contents of a file archived files. But unless you are a sys- slash (\) in front of it. For instance, \\ in- in an archive, with the same options tem administrator or an old Unix hand, dicates that you are looking for a back- as the cat command. chances are you will rarely see cpio slash, not a directory. The backslash is • bziprecover helps recover damaged - used. known as an escape character, and it sig- chived files. nals that the command should read what • bunzip2 decompresses files. Extending File Manage- follows literally, instead of as a glob. The differences between gzip and bzip2 ment with Globbing Globs can be especially useful when can be hard to remember, so many users One reason shell commands are so pow- you want a selected list from a directory prefer to rely on the tar command. The erful is that they can work with multiple full of files or when you are using one of tar command not only has the advantage files. With many commands, the easiest the grep commands to find content in- of having options to use gzip and gunzip way to work with multiple files by enter- side a file. However, you must be careful (‑z) or bzip2 (‑j), but it also offers far ing a space-delimited list directly after about using globs with commands like more control over exactly how you com- the command. However, the most con- rm or mv that change or rearrange the press files. cise and efficient way to handle multiple content of your hard drive. Otherwise, a In fact, tar's options run into the doz- files is through file globbing. command can have disastrous conse- ens – too many to detail here. For exam- File globbing refers to the use of regu- quences. To be safe, consider using a ple, you can use ‑ ‑exclude to ex- lar expressions (often abbreviated to newly constructed glob with the innocu- clude a file and ‑p to preserve the per- regex), pattern matching, metacharac- ous ls command, so you can see what missions of a file. If you want to preserve ters, or wild cards. The terms are not files it might affect. a directory structure, use ‑p. To be safe quite synonymous, although they are when decompressing, use ‑k to prevent mostly used as if they were. But, what- Learning that Pays any accidental overwriting of files. ever term you use, it refers to a string of File management commands have a long The tar command also includes its characters that can stand for many dif- history in Bash. During the course of own built-in utilities in many cases. To ferent strings. their development, they have accumu- add one archive to another, use the for- The most widely used glob in the Bash lated options the way ships accumulate mat tar ‑ ‑append ; to update an archive with newer any number of unknown characters. haphazardly. versions of files with the same name, This glob is especially useful when you However, often, the options are sim- use the ‑u option; or to compare the files want to find files that share the same ex- pler than they first appear. For example, in an archive with other files, use the tension. For instance, the command ls you can be fairly certain that most tar ‑ ‑compares . *.png lists all the .png graphics in the management commands will use ‑r to in- The fourth compression command, current directory. clude sub-directories and their contents cpio, has fallen out of favor in recent By contrast, a question mark (?) and ‑v to print a detailed description of years, probably because its format is stands for any single character. If you what they are doing to the terminal. non-standard. For example, to create an enter the command ls ca?.png, the list of Similarly, to force a command to work, archive with cpio, you have to pipe ls matches will include the files cat.png regardless of consequences, you gener- through it and specify the output to a and cab.png but not the file card.png, ally use ‑f. Adding the ‑i option, how- specific file with ls | cpio ‑o > . one after the ca. be confirmed by you before it happens. That said, cpio has even more options From these simple beginnings, globs Even with such hints, these commands than tar, including such powerful alter- can quickly become more elaborate. To can take a long time to master. natives as the ability to archive an entire specify specific characters, you can use In fact, for basic actions, they might square brackets, so that offer little more than a graphical file test[12].png locates files manager can. But, if you try to do some- test1.png and test2.png, but thing more intricate – such as specifying Figure 7: The file command identifies the format of files, not test3.png (Figure 8). Also, how symbolic links are going to be helping you identify them. you can specify a search for a treated or excluding a file from an ar- regex at the start (^) or the chive – the file management tools easily end ($) of a line. Similarly, outclass their desktop equivalents. If you you can search at the start of learn some of the less straightforward a word with \< or the end of options for these commands, you’ll soon a word with \> – and these understand why many experts prefer to Figure 8: All you need is a few regular expressions to are simply a few common use the command line for file manage- increase the flexibility of commands. Here, their use possibilities. Using globs is ment over anything that the desktop has greatly simplifies the finding of files. an art form, and experts to offer. n

linux shell handbook 11

009-011_filemanagement.indd 11 02/08/11 9:51:28 PM