In all operating systems, including Linux, the term “command” means either a command-line utility or a specific feature built into the system’s command shell. You enter a command in the terminal emulator and get the result of its execution.
Now let’s look at some basic utilities that everyone needs to know in order to effectively use BASH. Let’s go!
LS
LS is an utility for viewing the contents of directories. Displays the current directory by default. If you specify the path to a folder in the parameters, it displays the contents of this folder.
Useful options:
-a
shows all files and directories, including hidden ones-l
formats the output as a list with more detailed information*.m
shows all files with the extension “.m”*My*
shows all files and folders with names that contain “ My”
Usage examples:
ls
ls /home/dmfrpro/
ls -a -l /usr/share/applications/
ls /home/dmfrpro/*.txt
ls *Class*
CD
CD is an utility for switching from the current directory to the specified one. If you run it without parameters- it returns it to the home directory. A call with two dots cd ..
returns a level up relative to the current directory. Calling with a dash cd -
returns to the previous directory.
Useful options:
..
returns a level up relative to the current directory-
returns to the previous directory
Usage examples:
cd /home/dmfrpro/Downloads
cd ..
cd -
cd ../Projects
MKDIR
MKDIR is an utility for creating a new directory.
Useful options:
-p
creates a subdirectory structure, even if they don’t exist
Usage examples:
mkdir Projects
mkdir -p /home/dmfrpro/Projects/DemoProject/resources
TOUCH
TOUCH is an utility for creating a file. Using different options, you can change its properties, but we will talk about this in more detail in other articles.
Usage examples:
touch 1.log
touch /home/dmfrpro/properties.txt
CP
CP is an utility for copying files and directories to the specified directory. To copy a directory with all the files in it, you need to specify the -r
key for recursive copying.
Command syntax:
cp path/to/file path/to/destination/file
cp path/to/directory/ path/to/destination/directory
Useful options:
-r
recursively copies the files in the directory-a
saves user permissions
Usage examples:
cp 1.log Logs/Android/1.log
cp -r -a /home/dmfrpro/Logs /home/dmfrpro/Backup
MV
MV is an utility for moving (cut-paste) files and directories to the specified directory. To move a directory with all the files in it, you need to specify the -r
key for recursive movement. The same command is used to rename files.
Command syntax:
mv path/to/file path/to/destination/file
mv path/to/directory/ path/to/destination/directory
mv old_file_name new_file_name
Useful options:
-r
recursively moves the files in the directory-a
saves user permissions
Usage examples:
mv 1.log Logs/Android/
mv -r -a /home/dmfrpro/Logs /home/dmfrpro/Backup
mv 1.log 2.log
RM
RM is an utility for deleting files and directories. To delete a directory with all the files in it, you need to specify the -r
key for recursive deletion. Be careful when deleting files with superuser rights, as you can delete system files.
Useful options:
-r
recursively deletes the files in the directory-rf
recursive forcibly removes the files in the directory
Usage examples:
rm 1.log
rm -r /Backups
rm -rf /home/dmfrpro/Projects
FIND
FIND is an utility for searching files and directories.
Command syntax:
find path/to/directory/ --name file_name_or_directory_name
Useful options:
.
search in the current directory
Usage examples:
find . -name *.log
find / -name home
find /home/dmfrpro/ -name *Class*.txt
CAT and TAC
CAT is utility that prints the contents of a file to the console or to a file. TAC prints the content from the end to the beginning.
Command syntax:
cat file name
cat path/to/file_name_1 path/to/file_name_2
Usage examples:
cat 1.log
tac 1.log Backups/2.log
cat 1.log 2.log Backups/3.log
ECHO
ECHO is an utility that prints a line to the console or a file.
Command syntax:
echo string
echo “string” > path/to/file # prints the string to the file, pre-creating the file if it is not there
Usage examples:
echo “hello world”
echo “hello world” > 1.log
MAN
MAN is an utility that prints a manual for a given command to the console.
Usage examples:
man cat
man cd
PING
PING is an utility that prints ping (the delay in connecting to servers). Often used to check your internet connection.
Usage examples:
ping 192.168.0.1
ping google.com
SU and SUDO
SU is an utility that grants the user a superuser rights. SUDO only executes a command once on behalf of the superuser. Be careful when working with superuser rights!
Usage examples:
sudo mount /dev/sda1 /mnt
su
PUSHD and POPD
PUSHD is an utility similar to CD, but allowing you to remember the previous path. After its execution, two paths are output separated by a space: the new and the old.
With the POPD command, you can return to the old path from which the transition was made.
Usage Example:
/a$ pushd b
/a/b /a
/a/b$ popd
/a
/a$
Sources
This article is based on my previously made article in VK Unix Power group.
Source