Hey there! What’s your problem with command line interface of Ubuntu or other OS?
Don’t worry about it! I can solve your all basics and advanced problems of terminal based commands for Ubuntu OS.
Here are some commands to know about that to use your skill or proficiency into CUI.
1. ls command: List the contents of a folder or directory
Table of Contents
This command lets you see what files and folders are in your current folder.
How to use it – ?
1- Open your ubuntu terminal by pressing on keyboard keys – Ctrl + Alt + T
2- After opening your terminal window type ls
3- You can use the long listing option to see details like file size, permission, modified time, etc. type
ls -l
2. cd command: Change the directory
By default, you start in your home directory. You’ll often require to change the directory and move to another one.
How to use it – ?
First of all, recognize, in which directory, you want to move it or locate it by using ls command. After identified your directory or folder then type –
cd your_directory_name
3. cat command: Read a text file
If you quickly want to see the contents of a text file in Ubuntu, cat is the command you use. It displays the contents on the screen. if you need super user permission to run that command, type sudo cat instead of only cat command.
How to use it -?
First of all, you need to know about which files data you wanted to read it – after recognize, type –
cat file_name
or
sudo cat file_name
4. less command: Read a large text file
The cat command is good enough for viewing small text files. But I won’t recommend using cat if you have a huge text file with hundreds of lines.
When you open a file with less command, it opens the file in pages. You can scroll up/down, look for text, and more.
How to use it -?
First of all, you need to know about which files data you wanted to read it – after recognize, type –
less file_name
or
sudo less file_name
5. touch command: Create new files
There are multiple ways of creating new files in the Linux terminal. However, I prefer the touch command for this purpose.
How to use it -?
touch new_file_name
6. mkdir command: Make new folders
There is a dedicated command for making new folders (or directories, as we call them in Ubuntu).
How to use it -?
mkdir new_folder_name
7. cp command: Copy files and folders
Copying files and folders in the command line is also one of the common tasks you will encounter. The cp command, short for copy, is used for this purpose.
Imagine that you have to modify a configuration file. A smart move will be to copy the file with another name. This way, you’ll have a backup of the file.
cp existing_file backup_file
or for superuser permission
sudo cp existing_file backup_file
Note -: You can use the same cp command for copying directories as well. For that, you must specify the recursive option -r
sudo cp -r directory another_directory_location
8. mv command: Cut-paste or rename files and folders
The mv command moves the files and folders to the other location. You can think of it as a cut-paste operation.
mv file_name location_name
You can use the mv command to rename the file as well.
mv file1.txt new_file.txt
The same mv command also moves or renames folders without any special options.
mv new_folder_name old_folder_name
9. rm command: Remove files and folders
You use the rm (short for remove) command to delete files in the Linux terminal.
rm filename
Note -:There is no undo option after you delete files in the command line. This is why you should be extremely careful while deleting files. If you are afraid of deleting the wrong file, use the interactive mode with option -i, which gives you an additional prompt to confirm the action.
rm -i filename
With the recursive option -r, you can also use the same rm command to delete folders.
10. nano: Edit files
There are command line-based text editors for this purpose. You will have to use the keyboard shortcuts for moving around, making changes, saving, and exiting files.
To open a new, unnamed file with nano, use:
nano
To edit an existing file in Nano, use:
nano filename
11. clear: Clear terminal screen
Nano feels like a complicated one, right? Let me share a simple command.
The clear command clears the terminal. That’s it.
clear
12. ps: Check and handle processes
The ps command is for handling the processes running on your system. Each process has an associated ID called PID.
- PID: Process ID
- TTY: Controlling terminal associated with the process (Not that important these days)
- TIME: Total CPU usage time
- CMD: Name of command that runs the process
To see all the processes running by all users, use:
ps aux
This will give a massive list of processes and more details about them. If you run this command, now will be an excellent time to use the clear command.
13. top: System monitor
While the ps command gives you all the running processes, the top command gives you a real-time view of the processes and the system resource consumption.
top
Note – To stop the running top command, use the Ctrl + C keyboard shortcut.
14. lsblk: List disks and partitions
It displays the disks and partitions.
15. fdisk: List and manage disks and partitions
Another similar but better command is the fdisk command. It lets you manipulate the disk partitions. This means you can create new partitions and delete and resize existing ones with this command.
You can also use it to list all block devices, including loop devices on your system.
sudo fdisk -l
16. find: Search for files
Even as a desktop user, you’ll encounter cases where you may have to search for files in the Linux command line.
Here’s an example of the find command that will give you all the files that end with .txt extension in the current directory.
find . -type f -name “*.txt”
17. grep: Search in file content
The find command search for files based on their name and type. If you want to search based on the content of the files, you use the grep command.
So, instead of looking for all files ending with .txt, you look for all files containing the text ‘foss’ with grep.
grep -ri search_term
18. kill: Terminate processes
Violence is not the answer … it’s the solution.
Just kidding!
If you have a misbehaving process that takes too many system resources, you can find it and then terminate it using the kill command.
sudo kill -9 process_ID_or_name
As you can see in the above command, you need to know the process ID (PID) or the name to terminate it. You can use the ps or the top command to get the PID or exact process name.
ps aux | grep -i “name of your desired program”
19. history: Look back into what commands you ran in the past
So, you used a specific Linux command a few days ago. You need to rerun it, but you cannot recall it correctly.
You can press the up and down arrow keys.
Enter history in the terminal, and you should see a history of commands you ran in the past.
history | grep aux
There is another way to access the command history and search it.
20. chmod: Change file permissions
The chmod (change mode) command is used to change a file’s permissions.
chmod u+x file executable