Lecture 2 Practical
Lecture 2 Practical
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.
If for some reason you wish to create lots of files, then commands like these would be
very helpful:
And then use the ls command to see what all has been created.
This screenshot shows the difference between two files, one with upper case T, the other
with lower case t.
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 -a archive files
cp -f force copy by removing the destination file if needed
cp -n no file overwrite
➢ 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:
➢ If you want to just rename a file, you can use the mv command in the following way:
mv [filename] [new_filename]
mv test1.txt test7.txt
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 test7.txt /home/azad/Folder2
When you need to rename only one file then mv is the preferred command to use.