NDG LEv2 Ch08
NDG LEv2 Ch08
Objective Description
Create, move and delete files and directories under the home
directory
Introduction
Introduction
● In this chapter we will discuss how to manipulate files and
directories.
● These are symbol characters that have special meaning to the shell.
● Globs are powerful because they allow you to specify patterns that
match filenames in a directory:
○ Instead of manipulating a single file at a time, you can easily execute
commands that will affect many files.
Globbing - The Asterisk *
● The asterisk character is used to represent zero or more of any
character in a filename.
● For example, suppose you want to display all of the files in the
/etc directory that begin with the letter t:
sysadmin@localhost:~$ echo /etc/t*
/etc/terminfo /etc/timezone
● The pattern t* matches any file in the /etc directory that begins
with the character t followed by zero or more of any character.
Globbing - The Question Mark ?
● The question mark character matches exactly one character, no more and no
less.
● Suppose you want to display all of the files in the /etc directory that begin
with the letter t and have exactly 7 characters after the t character:
sysadmin@localhost:~$ echo /etc/t???????
/etc/terminfo /etc/timezone
● The asterisk and question mark could also be used together to look for files
with three-letter extensions by running the echo /etc/*.??? command:
sysadmin@localhost:~$ echo /etc/*.???
/etc/blkid.tab /etc/issue.net
Globbing - Brackets [ ]
● Brackets are used to match a single character by representing a range of
characters that are possible match characters.
● For example, echo /etc/[gu]* will print any file that begins with either
a g or u character and contains zero or more additional characters:
sysadmin@localhost:~$ echo /etc/[gu]*
/etc/gai.conf /etc/groff /etc/group /etc/group- /etc/gshadow /etc/gshadow- /etc/ucf.conf
/etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf
● For example, the command echo /etc/[!a-t]* will display any file
that does not begin with an a thru t:
sysadmin@localhost:~$ echo /etc/[!a-t]*
/etc/ucf.conf /etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf /etc/vim
/etc/wgetrc /etc/xml
Globbing - Listing With Globs
● Use the -d option, which tells the ls command to display the name of
directories, instead of their contents:
sysadmin@localhost:~$ ls -d /etc/e*
/etc/encript.cfg /etc/environment /etc/ethers /etc/event.d /etc/exports
Copying Files and
Directories
Copying Files
● The cp command is used to copy files. It requires a source and a destination.
● The source is the file to be copied. The destination is where the copy is to be
located.
● The following command will copy the /etc/hosts file to your home directory:
sysadmin@localhost:~$ cp /etc/hosts ~
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates hosts
Documents Music Public Videos
Copying Files - Verbose Mode
● The -v option will cause the cp command to produce output if successful.
● When the destination is a directory, the resulting new file will have the same
name as the original file.
● If you want the new file to have a different name, you must provide the new
name as part of the destination.
Copying Files - Avoid Overwriting Data
● The cp command can be destructive to existing data if the destination file
already exists.
● With the -i (interactive) option, the cp will prompt before overwriting a file
(y (yes) or n (no)):
● The -i option requires you to answer y or n for every copy which could be
tedious if there are a lot of files.
sysadmin@localhost:~$ cp -n /etc/skel/.* ~
cp: omitting directory `/etc/skel/.'
cp: omitting directory `/etc/skel/..'
● However, the -r (recursive) option to the cp command will have it copy both
files and directories.
Be careful with this option. The entire directory structure will be copied. This
could result in copying a lot of files and directories!
Moving Files
Moving Files
mv [source] [destination]
● When a file is moved, the file is removed from the original location and
placed in a new location.
● Note: If you don't have the right permissions, you will receive a "Permission
denied" error message.
Moving Files - Renaming Files
● The mv command is not just used to move a file, but also to rename a file.
● The name of the file will change only if a destination file name is also
specified.
● If a destination directory is not specified, the file will be renamed using the
destination file name and remain in the source directory.
● For example, the following commands will rename the newexample.txt file
to myexample.txt:
sysadmin@localhost:~/Videos$ mv newexample.txt myexample.txt
Moving Files - Additional mv Options
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
sample.txt
sysadmin@localhost:~$ mkdir test
sysadmin@localhost:~$ ls
Desktop Downloads Pictures Templates test
Documents Music Public sample.txt
Removing Files and
Directories
Deleting Files
● Using the rm could cause problems when deleting multiple files by using glob
characters.
● As a precaution, users should use the -i option when deleting multiple files.
Deleting Directories