Introduction:
Ever found yourself trying to manage files or folders on your computer? Believe me, I’ve been there too!
The Linux file system can indeed resemble a puzzle, especially if you’re just getting started. Sound familiar? Fret not! My goal is to simplify this journey and make it as delightful as exploring your own home.
In this blog, I’ll introduce you to the essential commands every Cloud Engineer should master. You’ll uncover the secrets of finding what you seek and, who knows, maybe even have a bit of fun along the way.
Excited? Let’s dive right in and embark on this exciting learning adventure together! Whether you’re a seasoned sysadmin or a curious newcomer, understanding key commands can make your Linux experience smoother and more productive.
Commands:
Let’s start with the first command.
1. Touch
The touch
command in Linux is primarily used to modify a file timestamp. It can also be used to create an empty file. When you work on your Linux computer, you may need to create an empty file so that applications can write to it. Also, certain administrative services may require files to have a certain timestamp.
The touch
command can be used with or without any options. When used without any option, the command creates an empty file if the file does not exist. If the file exists, then this command only changes the last access and modifies timestamps to the current system time.
The syntax for the command is,
Syntax: touch [options] file
Example: touch newfile.txt
Common Options:
-a:
Change the access time only.-c:
If the file does not exist, do not create it.-d:
Update the access and modification times.-m:
Change the modification time only.-r:
Use the access and modification times of the file.-t:
Creates a file using a specified time.
To create an empty file called “file1”.$ touch file1
To create multiple files at once.$ touch file1 file2 file3
To change the modification time of a file.$ touch -m file1
To use the timestamp of another file.$ touch -r file1 file2
To create a file with a specific timestamp.$ touch -t 202308161200 file1
The touch
command in Linux is a powerful tool for managing file timestamps and creating empty files. It can be used with various options to modify the access time, modification time, or both. This command is essential for any Linux user, whether you are a beginner or an experienced user.
2. Cat:
The cat
command in Linux is a widely used command that allows you to display the contents of one or more files, concatenate files, and create new files. The name of the cat
command comes from its functionality to concatenate files.
Syntax: cat [OPTIONS] [FILE_NAMES]
Example: cat file1.txt file2.txt
Common Options:
-n:
Number the lines of output.-b:
Number only non-blank lines of output.-s:
Squeeze multiple blank lines into one.-E:
Display a “$” at the end of each line.-T:
Display tab characters as “^I”.-v:
Display non-printable characters using “^” notation.--help:
Display the help message.
To display the contents of a single file.$ cat file1
To display the contents of multiple files.$ cat file1 file2
To concatenate the contents of two or more files and display them.$ cat file1 file2 > newfile
To append the contents of one file to another file.$ cat file1 >> file2
To number the lines of a file.$ cat -n file1
To display the contents of a file in reverse order.$ tac file1
To suppress repeated empty lines in the output.$ cat -s file1
These are just a few examples of how the cat
command can be used in Linux. There are many other options and use cases for this command, which you can explore by referring to the man pages. You will use the following command to access the manual pages of cat
the command.$ man cat
Let’s continue and discuss the third command.
3. cp (Copy)
The cp
command stands for “copy.” It allows you to copy files or directories from one location to another.
4. mv (move)
The mv
command stands for “move.” It allows you to move files or directories from one location to another. This command is useful when you want to organize files into different directories.
5. rm (remove)
The rm
command stands for “remove.” It allows you to delete files or directories. This command is useful when you want to delete files or directories that are no longer needed.
6. mkdir (Make Directory)
To create a new path on our journey, we use “mkdir.” It allows you to create a new directory. This command is useful when you want to create a new directory to store files.
7. more
The more
command is used for reading text files neatly and easily. It helps you look at a file bit by bit, just like flipping pages in a book. This is super helpful when you’re dealing with a lengthy file that’s too big to show all at once on your computer screen.
Syntax: more [options] [file]
Let’s see a few options (flags) with the more
command along with examples
-d or --dashes:
When you use this,more
add line numbers and dashes to help you keep track of where you are in the file.-f or --force:
This lets you usemore
even if you’re not directly typing into the terminal. It’s useful when you’re getting information from other programs.
This command instructs the computer to show you the contents of smallfile.txt
, allowing you to read it in segments of up to 10 lines at a time. It’s a helpful way to go through a file without getting overwhelmed by too much text all at once.
Remember, more
is a simple way to read through files, and for more advanced features and better usability. Let’s move to our next command.
8. Less
The less
command in Linux is a tool that helps you read and navigate through text files in a user-friendly and organized manner. It’s an improved version of the older more
command, offering more features and flexibility for viewing files.
Syntax: less [options] [file]
Let’s see a few options (flags) with the less
command along with examples
-N or --LINE-NUMBERS:
Displays line numbers on the left side of the screen.-i or --IGNORE-CASE:
Ignores case sensitivity when searching for text.
9. Head
The head
command in Linux is used to display the beginning or top part of a text file. It shows the first few lines of a file by default, but you can specify the number of lines you want to see. This is useful when you have a large file and you only want to see a preview of its contents.
Syntax: head [options] [filename]
Let’s see a few options (flags) with the head
command along with examples.
To display the first 10 lines of a file named “sample.txt”. This will show the first 10 lines of the “sample.txt” file.$ head -n 10 sample.txt
To display the first 20 bytes of a file named “data.bin”. This will show the first 20 bytes of the “data.bin” file.$ head -c 20 data.bin
Remember, the head
command is a helpful tool to quickly preview the beginning of a file’s contents without opening the whole file. It’s commonly used when you want to take a peek at the initial content of large files.
10. Tail
The tail
command in Linux is used to display the end or bottom part of a text file. It shows the last few lines of a file by default, but like the head
command, you can specify the number of lines you want to see. This can be handy when you want to check the most recent entries in a log file or view the end of a long text document.
Syntax: tail [options] [filename]
11. Echo
The echo
command in Linux is used to display text or messages on the terminal. It’s a simple way to print out information or create simple outputs in the terminal.
Syntax: echo [options] [text]
12. Find
The find
command in Linux is used to search for files and directories within a specified location based on various criteria, such as file names, types, sizes, and more. It’s like a digital detective that helps you locate files on your computer.
Syntax: find [starting_directory] [options] [search_criteria]
13. Grep
The grep
command in Linux is used to search for specific text patterns or words within files. It helps you find lines in files that match a certain pattern, making it like a powerful search tool for text.
Syntax: grep [options] pattern [file(s)]
Conclusion
And there you have it, the Linux adventurers! We’ve journeyed through a list of Linux commands, each one adding a new skill to yourself. From “touch” to “grep,” “find” to “echo,” and beyond, you now have the power to manipulate files and communicate with your Linux environment like a seasoned system administrator, developer, or Linux enthusiast!
These commands empower you to manage, search, and create within the world of Linux. So go forth and conquer, young tech enthusiasts!
Explore further, experiment fearlessly, and embrace the limitless possibilities that the Linux File System universe has to offer.
Happy command-line adventures!