In this chapter, we will delve into various commands and techniques for managing files and directories in Linux. Understanding these commands is crucial for efficiently navigating the file system, manipulating files, and performing routine tasks.
Section 1: Navigating the File System
1 ls (List):
This command is used to list files and directories in a specified directory.
Syntax:
ls [options] [file(s) or directory]
Options:
-a, --all
: List all files, including hidden files (those starting with .).-l, --long
: Use a long listing format, displaying detailed information about each file.-h, --human-readable
: Display file sizes in a human-readable format.-r, --reverse
: Reverse the order of the sort to get reverse lexicographical order or the oldest entries first.-t
: Sort files by modification time, newest first.-R, --recursive
: List subdirectories recursively.-F, --classify
: Append indicator characters to the end of each listed name to indicate the file type.-S
: Sort files by size, largest first.-i, --inode
: Print the index number of each file.
Examples:
1 List files and Directories in Current Directory:
ls
2 List All Files (Including Hidden) in Current Directory:
$ ls -a
3 List Files with Detailed Information:
$ ls -l
4 List Files in Human-Readable Formati:
$ ls -h
5 List Files in Reverse Order:
$ ls -r
6 List Files Sorted by Modification Time (Newest First):
$ ls -t
7 List Files and Subdirectories Recursively:
$ ls -R
8 List Files with Indicators (File Type):
$ ls -F
9 List Files Sorted by Size (Largest First):
$ ls -S
10 List Files with Inode Numbers:
$ ls -i
2 cd (Change Directory)
Allows you to change your current working directory.
Syntax:
cd [directory]
cd ..
= moves one directory upcd -
= switches to the previous directory
Example:
cd src
3 pwd (Print Working Directory)
The pwd command prints your current working directory’s path, like /home/directory/path.
Syntax:
pwd [option]
Example:
pwd
// Output
/home/user/Documents
Section 2: File Operations
1 cp (Copy)
Copies files or directories from a source location to a destination location.
Syntax:
cp [options] source destination
Example:
$ cp file1.txt /home/user/Documents/file2.txt
Copies file1.txt
to the /home/user/Documents/
with the name file2.txt
.
2 mv (Move)
Move files or directories from one location to another.
It can also be used to rename the file by providing the source and destination to same directory but with different name.
Syntax:
mv [options] source destination
Example:
$ mv file1.txt /home/user/Documents/file2.txt
This example moves the file1.txt
to /home/user/Documents
with the name file2.txt
.
For Renaming:
mv file1.txt file2.txt
3 rm (Remove)
Deletes files or directories. Use with caution as deleted files are not recoverable by default.
Use -r
option, to delete non-empty directory.
Syntax:
rm [options] file(s) or directory
Example:
$ rm file1.txt
4 mkdir (Make Directory)
Creates one or more directories with the specified names.
Syntax:
mkdir new_directory
Example:
mkdir new_directory
Creates a new_directory
in current working directory.
5 rmdir (Remove Directory)
Deletes empty directories. It cannot remove directories with contents unless the -r
option is used.
Syntax:
rmdir [options] directory_name(s)
Example:
6 touch
The touch
command is used to create a new empty files or update the timestamps of existing files.
Syntax:
touch [options] file(s)
Options:
-a
: Change the access time only.-c
: Do not create a new file if it does not exist.-m
: Change the modification time only.-d
: Update the access and modification times to the specified data and time.
Examples:
Create a New File:
touch newfile.txt
Updating Timestamps:
touch existingfile.txt
Updating Timestamps to a Specific Data and Time:
touch -d "2024-04-09 10:30:00" existingfile.txt
Changing Only the Access Time:
touch -a existingfile.txt
Changing Only the Modification Time:
touch -m existingfile.txt
Creating Multiple Files:
touch file1.txt file2.txt file3.txt
Preventing Creation of New file if Not Exist:
touch -c nonexistingfile.txt
7 file command
The file
command is used to determine the type of a file. It examines the file contents and provides information such as file type, encoding, and more.
Syntax:
file [options] file(s)
Options:
-b
: Brief output format (display only file type).-i
: Mime-type output format-z
: Uncompresses files before determining the file type.-L
: Follow symbolic links.
Examples:
1 Basic Usage
$ file myfile.txt
myfile.txt: ASCII text
2 Brief Output
$ file -b myfile.txt
ASCII text
3 Mime-type Output
$ file -i myfile.txt
myfile.txt: text/plain; charset=us-ascii
4 Determining the Type of Binary Executable
$ file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=..., stripped
5 Determining the Type of Archive
$ file myarchive.zip
myarchive.zip: Zip archive data, at least v2.0 to extract
6 Determining the Type of Compressed File
$ file -z mycompressedfile.gz
mycompressedfile.gz: gzip compressed data, last modified: ...
7 Following Symbolic Links
$ file -L mysymlink
mysymlink: symbolic link to myfile.txt
Section 3: File Manipulation
1 cat (Concatenate)
The cat command is used to concatenate and display the contents of one or more files to the standard output (usually the terminal).
Syntax:
cat [options] [file(s)]
Options:
-n
: Number all output lines.-b
: Number non-blank output lines.-s
: Squeeze multiple adjacent blank lines into a single blank line.-E
: Display a dollar sign ($) at the end of each line.
Examples:
1 Displaying a File's Contents
$ cat myfile.txt
This is the content of myfile.txt.
This command simply displays the content of the file named myfile.txt in the terminal.
2 Displaying Multiple File's Contents
$ cat file1.txt file2.txt
Contents of file1.txt
Contents of file2.txt
Here, cat displays the contents of both file1.txt and file2.txt sequentially.
3 Numbering All Output Lines
$ cat -n myfile.txt
1 This is line 1
2 This is line 2
With the -n option, cat numbers all the lines in the output, making it easier to reference specific lines.
4 Numbering Non-Blank Output Lines
$ cat -b myfile.txt
1 This is line 1
2 This is line 2
The -b option numbers only the non-blank lines in the output. Blank lines are ignored.
5 Squeezing Blank Lines
$ cat -s myfile.txt
This is line 1
This is line 2
With the -s option, cat squeezes multiple adjacent blank lines into a single blank line, making the output cleaner.
6 Displaying a Dollar Sign at the end of Each Line
$ cat -E myfile.txt
This is line 1$
This is line 2$
The -E option displays a dollar sign ($) at the end of each line, indicating the end of the line.
7 Appending the Content of a File to Another
$ cat file1.txt >> file2.txt
This command appends the content of file1.txt to the end of file2.txt, without overwriting the contents of file2.txt.
2 head and tail
- head Command:
- The head command displays the first few lines of a file.
- By default, it displays the first 10 lines of a file.
- It's useful for quickly previewing the beginning of a file.
tail Command:
- The tail command displays the last few lines of a file.
- By default, it displays the last 10 lines of a file.
- It's commonly used for monitoring log files, where new entries are appended to the end of the file.
Syntax:
head [options] [file(s)]
tail [options] [file(s)]
Head Command Options:
-n <num>
: Specifies the number of lines to display. Default is 10.-c <num>
: Specifies the number of bytes to display.-q
: Quiet mode. Suppresses headers when multiple files are specified.-v
: Verbose mode. Displays headers when multiple files are specified, which is the default behavior.
Tail Command Options:
-n <num>
: Specifies the number of lines to display. Default is 10.-c <num>
: Specifies the number of bytes to display.-f
: Follow mode. Displays new lines appended to the file in real-time.-F
: Similar to -f but handles file rotation (e.g., log rotation).
Head Examples:
1 Display the First 10 Lines of a File:
$ head myfile.txt
2 Display the First 5 Lines of a File:
$ head -n 5 myfile.txt
3 Display the First 100 Bytes of a File:
$ head -c 100 myfile.txt
4 Display the First 10 Lines of Multiple Files:
$ head file1.txt file2.txt
5 Display the First 10 Lines Quietly (Suppress Headers):
$ head -q file1.txt file2.txt
Tail Examples
1 Display the Last 10 Line of a File:
$ tail myfile.txt
2 Display the Last 5 Lines:
$ tail -n 5 myfile.txt
3 Display the Last 100 Bytes of a File:
$ tail -c 100 myfile.txt
4 Display the Last 10 Lines of Multiple Files:
$ tail file1.txt file2.txt
5 Display New Lines Appended to a File (Follow Mode):
$ tail -f myfile.log
3 grep (Global Regular Expression Print)
Searches for a specific pattern n one or more files and prints matching lines.
Syntax:
grep [options] pattern [file(s)]
Example:
Let the file contains the below contents.
This is the first line.
This is the second line.
This is the last line.
grep "second" file.txt
// Output
This is the second line.