- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
10 Interesting Linux Command Line Tricks and Tips
The command line interface (CLI) is an important feature of Linux operating systems. Although it may seem intimidating at first, it can be a powerful tool for performing various tasks efficiently. In this article, we will discuss some interesting Linux command line tricks and tips that can make your experience with the CLI smoother and more enjoyable.
Navigating the Command Line Interface
The command line interface can be intimidating for beginners, but mastering a few simple commands can make navigating the interface much easier. Here are a few tips to get you started −
Changing Directories
To navigate through the directories on the command line, use the "cd" command. For example, to change to the home directory, type −
cd ~
To change to a directory named "my_directory," type −
cd my_directory
To change to the parent directory, type −
cd ..
Listing Contents
To list the contents of a directory, use the "ls" command. For example, to list the contents of the current directory, type −
ls
To list the contents of a specific directory, type −
ls /path/to/directory
Tab Completion
Tab completion can save you time when typing long file or directory names. To use tab completion, type part of the file or directory name and then press the "Tab" key. The CLI will automatically complete the name for you.
Working with Files and Directories
Working with files and directories on the command line can be faster than using a graphical user interface. Here are some tips to help you manage your files and directories more efficiently −
Copying and Moving Files
To copy a file from one location to another, use the "cp" command. For example, to copy a file named "file.txt" from the current directory to a directory named "my_directory," type −
cp file.txt my_directory/
To move a file from one location to another, use the "mv" command. For example, to move a file named "file.txt" from the current directory to a directory named "my_directory," type −
mv file.txt my_directory/
Creating Directories
To create a new directory, use the "mkdir" command. For example, to create a new directory named "my_directory," type −
mkdir my_directory
Removing Files and Directories
To remove a file, use the "rm" command. For example, to remove a file named "file.txt," type −
rm file.txt
To remove a directory and all its contents, use the "rm" command with the "-r" option. For example, to remove a directory named "my_directory" and all its contents, type −
rm -r my_directory
Finding Files
To find files on the command line, use the "find" command. For example, to find all files with the extension ".txt" in the current directory and its subdirectories, type −
find . -name "*.txt"
Working with Processes
The Linux command line allows you to manage processes running on your system. Here are some tips to help you manage processes more efficiently −
Checking Running Processes
To check the processes currently running on your system, use the "ps" command. For example, to display a list of all processes running on your system, type −
ps -ef
Killing Processes
To kill a process, use the "kill" command. For example, to kill a process with the process ID "1234," type −
kill 1234
Background and Foreground
Sometimes, you may want to run a process in the background so that you can continue using the command line interface. To do this, add an ampersand ("&") at the end of the command. For example, to run a process named "my_process" in the background, type −
my_process &
To bring a background process back to the foreground, use the "fg" command. For example, to bring a process with job ID "1" to the foreground, type −
fg %1
Miscellaneous Tips
Here are some additional tips that may come in handy −
Viewing Command History
To view your command history, use the "history" command. For example, to display a list of your 10 most recent commands, type −
history 10
To repeat a previous command, use the exclamation point ("!"). For example, to repeat the last command you typed, type −
!!
To repeat a specific command from your history, type the command number preceded by an exclamation point. For example, to repeat command number 123, type −
!123
Using Aliases
You can create aliases for commonly used commands to save time. To create an alias, use the "alias" command. For example, to create an alias for the "ls" command so that it always displays file sizes in human-readable format, type −
alias ls='ls -lh'
To make the alias permanent, add it to your .bashrc file.
Using Wildcards
Wildcards can be used to match multiple files or directories with a single command. Here are some examples −
"*" matches any number of characters, including none. For example, to list all files with the extension ".txt," type −
ls *.txt
"?" matches any single character. For example, to list all files with names that begin with "file" and end with a single character, type −
ls file?
Using Pipes
Pipes allow you to take the output of one command and use it as input for another command. For example, to display a list of all files in the current directory sorted by size, type −
ls -l | sort -k5
This will first list all files in the current directory using the "ls" command and then pass the output to the "sort" command, which will sort the files by size.
Using the "grep" Command
The "grep" command allows you to search for specific text in a file or output. For example, to search for the word "error" in a log file named "logfile.txt," type −
grep "error" logfile.txt
This will display all lines in the file that contain the word "error."
Using the "tar" Command
The "tar" command allows you to create and extract compressed archive files. For example, to create a compressed archive file named "my_archive.tar.gz" containing all files in a directory named "my_directory," type −
tar -czvf my_archive.tar.gz my_directory/
To extract the contents of the archive file, type −
tar -xzvf my_archive.tar.gz
Using the "ssh" Command
The "ssh" command allows you to remotely access another computer over a secure connection. For example, to access a remote computer with the IP address "192.168.1.100" using the username "user," type −
ssh user@192.168.1.100
This will prompt you for the password for the specified user on the remote computer.
Using the "curl" Command
The "curl" command allows you to transfer data from or to a server using various protocols, including HTTP, FTP, and SMTP. For example, to download a file from a website, type −
curl -O https://example.com/file.zip
This will download the file and save it in the current directory.
Using the "df" Command
The "df" command displays information about the available disk space on the system's file systems. For example, to display information about the available disk space on the root file system, type −
df /
This will display information about the available disk space, the used space, and the total space on the root file system.
Using the "du" Command
The "du" command allows you to estimate file space usage. It displays the disk space used by files and directories in the current directory or a specified directory. For example, to display the disk space used by all files and directories in the current directory, type −
du
To display the disk space used by a specific directory, type −
du /path/to/directory
Using the "sudo" Command
The "sudo" command allows you to execute commands with administrative privileges. This is useful when you need to perform tasks that require elevated permissions. For example, to install a package using the "apt" package manager with administrative privileges, type −
sudo apt install package_name
This will prompt you for your password and then install the package with administrative privileges.
Conclusion
The command line interface can be a powerful tool for performing various tasks efficiently. With these Linux command line tricks and tips, you can navigate the interface more easily, manage your files and directories more efficiently, and work with processes more effectively. With practice and experience, you can become a command line ninja and impress your colleagues with your skills.