Lecture 2: - Working with Files
In this lecture we learn how to recognize, create, remove, copy and move files using
commands like file, touch, rm, cp, mv and rename.
2.1. touch
2.1.1. create an empty file
One easy way to create an empty file is with touch. (There are other ways for creating
files). This screenshot starts with an empty directory, creates two files with touch and the
lists those files.
2.1.2. Create multiple files with touch
To create multiple files, specify their names together separated by a space
2.1.3. Create lots and lots of files
If for some reason you wish to create lots of files, then commands like these would be
very helpful:
# Create files with names A to Z
$ touch {A..Z}
# Create files with names 1 to 20
$ touch {1..20}
# Create files with extension
$ touch {1..1000}.txt
# Create 10K files
$ touch {1..10}{1..1000}
And then use the ls command to see what all has been created.
2.2. all files are case sensitive
Files on Linux (or any Unix) are case sensitive. This means that FILE1 is different from
file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical
Linux computer).
This screenshot shows the difference between two files, one with upper case T, the other
with lower case t.
2.3. everything is a file
A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal
window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1) and
any process are all represented somewhere in the file system as a file. It will become clear
throughout this course that everything on Linux is a file.
2.4. file
The file utility determines the file type. Linux does not use extensions to determine
the file type. The command line does not care whether a file ends in .txt or .pdf. As a system
administrator, you should use the file command to determine the file type. Here are some
examples on a typical Linux system.
The file command uses a magic file that contains patterns to recognize file types. The
magic file is located in /usr/share/file/magic.
2.5. rm
2.5.1. remove forever
When you no longer need a file, use rm to remove it. Unlike some graphical user
interfaces, the command line in general does not have a waste bin or trash can to recover
files. When you use rm to remove a file, the file is gone. Therefore, be careful when removing
files!
2.5.2. rm -i
To prevent yourself from accidentally removing a file, you can type rm -i.
2.5.3. rm -rf
By default, rm -r will not remove non-empty directories. However rm accepts several
options that will allow you to remove any directory. The rm -rf statement is famous because
it will erase anything (providing that you have the permissions to do so). When you are
logged on as root, be very careful with rm -rf (the f means force and the r means recursive)
since being root implies that permissions don't apply to you. You can literally erase your
entire file system by accident.
2.6. cp
2.6.1. CP Syntax and Options
syntax
Copy from source to destination
$ cp [options] source dest
cp command main options:
option description
cp -a archive files
cp -f force copy by removing the destination file if needed
cp -i interactive - ask before overwriting
cp -l link files instead of copy
cp -L follow symbolic links
cp -n no file overwrite
cp -R recursive copy (including hidden files)
cp -u update - copy when source is newer than dest
cp -v verbose - print informative messages
2.6.2. copy one file
To copy a file, use cp with a source and a target argument.
syntax
Copy from source to dest
$ cp [options] source dest
2.6.3. copy from specific directory to another
directory
If the target is a directory, then the source files are copied to that target directory.
2.6.4. cp command examples
➢ Copy 3 files [Link], [Link] and [Link] to destination absolute path
directory /home/azad/Folder2/ :
➢ Copy all txt files in current directory to subdirectory Folder1 :
➢ Copy directory Folder1 to absolute path directory /home/azad/Folder2/ :
➢ Copy all files and directories in Folder1 recursively to absolute path
directory /home/azad/Folder2/ :
➢ Force file copy:
➢ Interactive prompt before file overwrite, i.e. to prevent cp from overwriting
existing files, use the -i (for interactive) option.
➢ Update all files in current directory - copy only newer files to destination
directory /home/azad/Folder2/ :
2.7. mv
2.7.1. rename files with mv
The mv command in Linux is used to move or rename files. Following is the syntax
of the command:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
➢ If you want to just rename a file, you can use the mv command in the following way:
mv [filename] [new_filename]
For example: for Rename FILE
mv [Link] [Link]
: for Rename DIRECTORY
mv Folder1 Folder3
➢ Similarly, if the requirement is to move a file to a new location, use the mv command
in the following way:
mv [filename] [dest-dir]
For example:
mv [Link] /home/azad/Folder2
When you need to rename only one file then mv is the preferred command to use.
For Homework: RENAME Command