In the previous article, ItsmeIT provided a compilation of terminal command in Ubuntu and Linux, along with real examples. Continuing from that, this article will cover the most useful and commonly used basic command in Linux or Ubuntu that are used by developers. These commands will help you perform tasks faster and more efficiently. Let ItsmeIT introduce you to these basic commands to enhance your working skills!
Basic Syntax of Linux Commands with Examples
The command pwd (print working directory) is one of the most basic command in linux and Unix, used to display the absolute path of the current directory you are standing in in the terminal.
For example, when you type the command
pwd in the terminal, the system will return the absolute path of the current directory. This is useful when you want to know your current location on a Linux file system, or when you want to use that absolute path to execute other commands.
ls is one of the most basic command in linux, used to list the contents of the current or specified directory.
Here are some common options for the “ls” command:
- “-l”: displays a list of files and folders in a detailed list view, including information about permissions, size, modification time, and names of files and folders.
- “-a”: displays a list of files and directories including hidden files and directories (with a dot) in the current or specified directory.
- “-h”: displays the size of files and folders in readable units, such as KB or MB, for better readability.
- “-S”: sort the list of files and folders by size from large to small.
- “-t”: sort the list of files and directories by last modified time.
- “-r”: reverses the sort order of the list of files and directories.
You can use the command in Linux with one or more of these options together to customize how the list of files and folders is displayed and sorted to your liking.
To illustrate basic command in Linux in more detail, ItsmeIT will provide an example with the combination of “-l”, “-a” and “-h” options in the “ls” command. ItsmeIT will get all files and folders, including hidden files and folders, with full information about access rights, number of links, owner, group, size and modification date, and the size will be displayed in readable units for easy reading and comparison.
The basic command in Linux for changing directories, cd, is used in Ubuntu and other Linux systems to move between different directories in the system. Using the command cd, you can enter the absolute path or relative path to the folder you want to move to.
Syntax to use:
It allows you to create one or more directories anywhere you want in your Linux system with a command in Linux, of course you must have the necessary permissions.
Syntax to use:
mkdir [option] [folder name]
- [folder name]: the name of the folder you want to create a new one.
- [options]: are options used to adjust the execution of a command. Some common options of the mkdir command include:
- -p : Create parent directory if parent directory does not exist.
- -m: Set permissions for the newly created directory.
- -v: Print to the screen information about the process of creating a new folder.
For example, to create a folder named “documents”, you can use the following command:
If you want to create a “photos” folder in the “documents” folder, you can use the following command in Linux:
If the “documents” parent directory does not exist yet, you can use the “-p” option to create a parent directory and a subdirectory at the same time:
mkdir -p documents/photos
rmdir in Linux or Ubuntu is used to delete empty directories (that do not contain any files or subdirectories). If the directory contains files or subdirectories, the command
rmdir will not work and an error message will appear.
Syntax to use the command
rmdir [option] [folder]
Some commonly used options in the command
-p: Delete empty parent directories if subdirectories have been deleted successfully.
-v: Displays a message on the screen when deleting a folder.
Example to delete a directory:
rmdir -p documents
Contrary to Windows, whenever you plug in an SD card or USB, chances are your distro won’t show them directly at the start. You need to mount them to your existing file system using the mount command in Linux. For example, to mount itsmeit directory to /dev/sdb1, you can use the command:
sudo mount /dev/sdb1 /itsmeit
The (disk free) command
df is one of the most powerful Linux commands that displays essential information about the amount of disk space on your file system. It is widely used by system administrators to monitor and analyze real-time server or network-oriented systems.
The syntax of the df command is as follows:
df [option] [directory]
Some common options of the “df” command include:
- -h, –human-readable: displays the capacity in a human-readable format.
- -T, –print-type: displays the file system type of the partitions.
- -i, –inodes: display the number of inodes used and the number of inodes remaining.
The path is used to specify a special partition to display information. If no path is provided, the “df” command will display information of all disk partitions on the system.
For example, to display information about all disk partitions on the system in a human-readable format, you can use the following basic command in linux:
This command will display the used space, free space and total space of all disk partitions on the system, including information about the swap partition (if any).
If you want to display information about a particular partition, you can specify the path to that partition as follows:
df -h /dev/sda1
uname is one of the basic Linux or Ubuntu commands to get system information like name, version and other system-specific details. You can quickly check your operating system and kernel version with basic command in linux this and can know for sure the command length on your machine.
The syntax of the “uname” command is as follows:
Some common options of the “uname” command include:
- -a, –all: display all system information.
- -s, –kernel-name: display the name of the operating system (kernel).
- -r, –kernel-release: show the version of the kernel.
- -v, –kernel-version: show the version number of the kernel.
- -m, –machine: show hardware architecture.
- -p, –processor: display information about the processor.
- -i, –hardware-platform: display the hardware platform name.
- -o, –operating-system: display the operating system name.
- -n, –nodename: will display the name of the network node on the system.
- -a, –all: displays all possible information about the system, including system name, kernel version, hardware architecture, manufacturer name, processor information, hardware platform name hardware and operating system name.
For example, to display all system information, you can use the following command:
The (concatenate) command
cat is one of the most frequently used Linux & Ubuntu command in Linux/Unix-like operating systems. The “cat” command is used to display the contents of one or more files on the screen.
The syntax of the “cat” command is as follows:
cat [option] [file]
Some common options of the “cat” command include:
- -n, –number: display the line number for each line in the file.
- -E, –show-ends: display the end-of-line character.
- -T, –show-tabs: display the tab character.
- -v: display non-printing characters.
For example, to display the information in the file “/etc/os-release”, you can use the “cat” command as follows:
Or another example showing the system kernel version information, including kernel version name, version number, release date, and other information.
kill are an effective way to stop processes that are stuck due to resource constraints. In short, you want to stop any active process, then
kill is the solution. Can kill by pid or kill all, need to use sudo or root privileges.
Syntax to use:
kill [option] [process ID or process name]
Some common options of the “kill” command include:
- -s, –signal: specifies the signal to be sent to the process. The default is the TERM (terminate) signal.
- -l, –list: lists all signals that can be used with the “kill” command.
- -p, –pid: specify the process ID instead of the process name.
Example of finding
pidand stopping a running application:
ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 167888 11836 ? Ss 09:12 0:06 /sbin/init sp root 2 0.0 0.0 0 0 ? S 09:12 0:00 [kthreadd
For example kill pid is 1 and 2
sudo kill -9 1 2
Another example of how to use kill command in Linux, Ubuntu:
ps aux | grep firefox
Then, to send the TERM signal to all processes named “firefox”, you can use the “killall” command as follows:
killall -TERM firefox
service command is a service management tool on Linux-based operating systems such as Ubuntu, CentOS, Debian, Red Hat, etc. This command allows users to start (start), stop (stop), restart (restart) or display the status (status) of system services.
The syntax of the “service” command is as follows:
service [ service name ] [ operation ]
An example to check the working status of nginx.
sudo service nginx status
shutdown is used to shutdown or restart a Linux-based system. This command requires administrative privileges
root to execute and can be used to schedule Linux shutdown or immediate shutdown.
The syntax of the “shutdown” command is as follows:
shutdown [ option ] [ time ] [ message ]
In it, options may include:
-h: Shut down the system after closing processes.
-r: Restart the system.
-c: Cancel the shutdown command.
-k: Send shutdown notifications to all logged in users without shutting down the system.
Times can be specified in the following formats:
now: Shut down the system immediately.
+m: Turn off the system after m minutes from the current time.
hh:mm: Turn off the system at time hh:mm.
For example, to shut down the system immediately, you can use the following command:
To shutdown the system after 10 minutes, you can use the following command:
To reboot the system immediately, you can use the following command:
shutdown -r now
touch command in Linux is used to create a new empty file or update the access and modification times of an existing file.
The syntax of the “touch” basic command in linux is as follows:
touch [ options ] [ filename ]
In which, filename is the name of the file you want to create or update time. Commonly used options include:
-a: Update file access time only.
-m: Update file modification time only.
-c: Update file time only if file already exists.
-r: Copy access time and modification time from one file to another.
For example, to change the access time of a file to the current time, use the option
-a before the filename with the tocuh command as follows:
touch -a test.txt
Another example to use the touch command to change the test.txt file time to 11/05/2023 at 10 hours 47 minutes 30 seconds. The command
touch also allows to set a certain time for access and editing time.
touch -t 202305111047.30 test.txt
The date and time structure should be in the correct format CCYYMMDDhhmm.ss, as described below:
- MM – month of the year [01-12]
- DD – day of the month [01-31]
- hh – hour of the day [00-23]
- mm – minutes [00-59]
- CC – the first 2 numbers of the year
- YY – the last 2 digits of the year
- SS – number of seconds [00-59]
The Linux command
head is used to display the first content of one or more text files. (default is 10 lines) and return data to display on the terminal screen . For example, display the first 10 lines of a file.
The command syntax
head is as follows:
head [ optional ] [ filename ]
For example using the command in Linux with the command
head to display the first 5 lines of the file “test.txt”, you can use the following command:
head -n 5 test.txt
You can also use the command
head to view the contents of multiple files at once. For example, to display the first 10 lines of the files “example1.txt”, “example2.txt” and “example3.txt”, you could use the following command:
head example1.txt example2.txt example3.txt
tail command is one of the useful basic command in Linux for displaying the last (n) number of data from the given input. As the name implies, the “tail” command outputs the last few lines of a file. By default, it prints the last 10 lines of the files specified in Linux. If multiple file names are provided, the file name will be preceded by the data from each of those files.
The command syntax
tail is as follows:
tail [ option ] [ filename ]
By default, the command
tailwill display 10 lines, but this parameter can be changed via the option .
tail -n 3 test.txt
tailalso allows to display the number of bytes, for example to display the last 500bytes of the file.
tail -c 500 filename.txt
Default is in bytes, if you want to convert to kilobytes, use the following structure:
tail -c 2k filename.txt
2k is 2kilobytes, which is 2048 bytes.
- The command
headdisplays data from the beginning of the file.
- The command
taildisplays data from the end of the file.
The “cp” command is one of the useful basic command in Linux allows copying a file or directory from one directory to another. It’s among the other commonly used Linux & Ubuntu commands that you just can’t live without. You can copy multiple files to a folder on your terminal using the cd command.
Some options of the cp command:
-r: copy folder and contents inside.
-i: confirm before overwriting the target file if the destination file already exists.
-v: displays a detailed message about the copying process.
cp -R file-or-directory /destination/directory
Or copy many different files or folders
cp -R option1 option2 ... /destination/directory
For example, if you want to see the details of the copying process, you can use the
-v. For example, to copy the file “file.txt” from the current directory to the directory “/home/$USER/documents” and display a detailed message about the copying process, you can use the following command :
cp -v file.txt /folder/directory/need/move/to
cp, with the command in Linux with the option
mvyou can move one or more files or folders from one location to another. You can use
mvthis command to transfer large files with the
Here are some common options used with the command
-i: Confirm before overwriting the target file if the destination file already exists.
-f: Overwrite the target file without asking for confirmation from the user.
-n: Do not overwrite the destination file if the destination file already exists.
-u: Only move source files whose content is newer than the destination file or the destination file does not exist.
-v: Displays a detailed message about the move or renaming process.
--backup: Make backup copies of target files before overwriting them.
--target-directory: Specify the destination directory to move the source files to, instead of giving the path to the destination file.
For example, to move all .txt files to the
Documents folder, you can use the command:
mv *.txt ~/Documents/
To move multiple files at once to different directories, you can use the command:
mv file1.txt file2.txt file3.txt ~/Documents/
To move the file file.txt to the Documents folder and confirm before overwriting the target file (if any), you can use the command:
mv -i file.txt ~/Documents/
ln command is one of the most handy Linux & Ubuntu commands for creating symbolic links to some specific files. You can use this small but flexible command to create multiple instances of a symlink to a specific file or folder on your disk space. It is roughly the same as the shortcut on windows.
ln -s my_file.txt my_link.txt
To delete symlinks on Linux, ItsmeIT use the unlink or rm command.
history command is used to display the history of commands executed at the current user. Allows to quickly see what has been done previously on the system.
In commands in Linux like Ubuntu or debian, the command is quite special 2 exclamation points (!!), the command
!! is used to execute the previous command that you executed without having to retype the whole command. . When you type “!!” go to terminal and press Enter, the last command will be executed again.
wget is one of the best commands in Linux and Ubuntu to download files from the web right on the terminal. It is very handy in scripts or
cronjobs and supports HTTP, HTTPS and FTP internet protocols.
For example, to clone a GitHub repository with an HTTPS path of https://github.com/username/repository.git into the current directory, you can use the following basic command in Linux:
cURL stands for Client URL. This is a command line tool and library used to test connections from URLs and allow data to be transferred through a variety of protocols. Supported protocols: HTTP and HTTPS, FTP and FTPS, IMAP and IMAPS, POP3 and POP3S, SMB and SMBS, SFTP, SCP, TELNET, GOPHER, LDAP and LDAPS, SMTP and SMTPS
The simplest use of it
curl is to display the content of the page. The following example will use the curl command on Ubuntu to show you the contents of the homepage of https:// itsmeit.co. It will render for you the source code of this website homepage.
curl https:// itsmeit.co
You can also use curl to download files just like wget
curl -O url
curl can also use PHP to GET and POST data, access REST API services that incorporate JSON.
Above is a summary of the most useful and commonly used commands in linux and ubuntu. To better understand, you can use the man command with the syntax:
man command_name to get complete information about the function and syntax of that command.