File Power Folder and Move Files to It Instead of Using Rm Or Rmdir

File Power Folder and Move Files to It Instead of Using Rm Or Rmdir

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 command, 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 Penhallow, Kevin 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, compress- 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 operating system. 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 Unix 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 cp 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 <options> <filesourcefile> move a file with mv, which takes many move around the directory tree with the <target>. By default, cp overwrites any of the same options as cp. Also, you can command cd <directory> – 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 tar, gzip, bzip2, 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 <num- give you the equivalent of the back and many options that I will list only some of ber>, or to set the speed of compres- forward buttons in a web browser. the most important ones: sion, you can use - -fastest <number>. Once you are in a directory, use ls to • -amin <minutes>: 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 find that ls is actually an alias of • -cmin <minutes>: 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>: 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>: 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 <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 <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 <number>: 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 <number>: 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 <permissions>: 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 <filetype>: 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).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us