We shall learn about below below topics in this chapter:
3.1 Type of files in Linux
3.2 Linux file
3.3 Linux touch
3.4 Linux rm
3.5 Linux cp
3.6 Linux mv
3.7 Linux chmod
3.8 Linux readelf
3.9 Linux objdump
3.1 Type of files in Linux
There are 7 different files in Linux:
1. Regular files
2. Directories
3. Character Device files
4. Block Device files
5. Local device socket files
6. Named Pipes
7. Symbolic links.
1. Regular files:
Regular files can be day to day usable files like word document, spreadsheets, program executable files. Regular files are appended by “-“ when you check using “ls –l” command. In the image below you can see that the first character is an “-“.
2. Directories
Directory can be considered as a folder that holds other files. Directories can be appended by “d” when checked using “ls –l” command. Directories will be in blue colour as shown in the image below.
3. Character Device and Block Device files
They represent the devices that are attached to the system. The only place that we find the device files are in the “/dev” directory. The character device files are represented by “c” and block device files are represented by “b”. Block device files will be represented by light yellow and character device files are represented by dark yellow colour. Below image shown both of the device files.
4. Local device socket files:
They are the files which help the processes inside the operating system to communicate with each other. They are represented by “s” in the beginning. They are represented by purple in colour.
5. Named Pipes.
They are used for network communications. We can find them in “/run” directory. They are in yellow colour. They are represented by “p” in the beginning. Below is the image for the same.
6. Symbolic links.
They are the shortcut for a file or a directory. They are represented by “l”. Some of them can be found in “/dev” folder. They are light blue in colour. Below is the image.
3.2 Linux file
3.2.1 Usage:
File command is used to determine the type of the file.
3.2.2 Options:
-b Brief mode. It will display only the type of file without the file name.
-i –mime Displays the mime type of the file.
-z Look inside a compressed files.
* Displays the type of all the files.
3.2.3 Example:
1. To know type of file. Use “file <file_name>”.
2. To know the file name in brief mode. “file –b <file_name>”.
3. To know the mime type of the file. “file –i <mime_type>”
4. To know the file type inside a compressed file. “file –z <file_name.tar>”
5. To know the file types of all the files inside a directory. “file *”.
3.3 Linux touch
3.3.1 Usage:
Touch command is used to create, modify date, time of a file.
3.3.2 Options:
-a Change the access time only.
-c If the file doesn’t exist then, don’t create a new file.
-d update access and modified time
-m change modified time only
-t create file using specified time. [YYDDHHMM]
3.3.3 Example:
1. Create a new file. “touch hello.txt”
2. Change the access time. “touch –a hello.txt”. By using “stat” command we can see the access time and modified time. File was accessed and modified at “12:08”. After running the “touch –a hello.txt”, the access time has been changed to “12:10”.
3. Change the access and modified time. “touch –d hello.txt”. Same explanation as above, but additional to changing accessed time, it will also change the modified time.
4. Create a file using specified time YYDDHHMM. “touch –t test.txt”
5. Don’t create new file, if it is not exist. “touch –c test_1.txt”
3.4 Linux rm
3.4.1 Usage:
“rm” command is used to delete files and directories in Linux.
3.4.2 Options:
-f Force remove all the files and sub directories specified.
-i Prompt before deleting a file.
-r Delete directories recursively.
-d remove empty directory
3.4.3 Example:
1. Remove file. “rm hello.txt”.
2. Force remove files and directory including sub directories. “rm –fr test_dir”.
3. Ask confirmation before deleting any file every time. “rm –i hello.txt”.
3.5 Linux cp
3.5.1 Usage:
“cp” command is used to copy files and directory from one place to another. Copying the files from
3.5.2 Options:
-a Copy the file and retain the metadata of the file as much as possible.
–attributes-only Don’t copy the file, create the file and copy the attributes. If the file already exists then update the attributes.
-f Force copy, if there is a file already exist at the destination, then delete it and copy the file.
-r Copy the directory contents recursively.
-v Verbose mode. Display information after completion of the command.
-u Update, when source is newer than the destination
3.5.3 Example:
1. Simple Copy. “cp source.txt destination.txt”
2. Copy related files. “cp *.txt myDir”.
From the image below, we have copied the files ending with “txt” extension to “myDir” directory.
3. Copy in interactive mode. If the destination file already exists, then it will prompt a message. “cp -i source.txt destination.txt”.
In the image below, as there is already file called as “copied_file.txt”, it will ask for confirmation before replacing the file.
3.6 Linux mv
3.6.1 Usage:
“mv” command is used to move the file from source to destination. It is like cut and paste. It can also be used to rename a file or directory.
3.6.2 Options:
-b Backup the destination file if it exists. The backup file will be appended by “~” symbol.
-f If the destination file is read only, force option will forcefully replace the destination file and delete the original file. If “-f” option is not used, then “mv” command will ask for confirmation before replacing the destination file, if it is read only file.
-i Interactive mode, will prompt before overwriting an existing file.
-n Never overwrite an existing file.
-u Never update an existing file if it is newer. If the existing file is older than the source file, then overwrite the destination file.
-v Print the output of the operation.
–help Display help message
–version Display version information.
3.6.3 Example:
1. mv –v source.txt destination.txt
3.7 Linux chmod
3.7.1 Usage:
chmod is used to change the ownership of files and directories. There are 3 types of
User Permission: It will grant access to single user. He will be the owner or creator of the file.
Group Permission: This permission applies to group of users; they have access to the file.
Other Permission: This permission applies to all the users present in the system.
3.7.2 Example:
1. Let us consider the example “chmod 754 file.txt”.
The number “7, 5, 4” individually represents permission for “user”, “group”,
3.8 Linux readelf
3.8.1 Usage:
In Linux, whenever you compile a program and get the object file, it will be in ELF format. “readelf” command is used to read the ELF details of the file. “readelf” and “objdump” makes sense if you are a programmer. It is good to know.
3.8.2 Options:
-a Displays all the information about the file.
-h Displays the header information
-l Display the program headers
-s Display the symbol table of the file.
3.8.3 Example:
For the example, I have written a C program and compiled and got the object file. So we shall see what the command will give the output for different options.
1. readelf –a hello_world.o