Bash is the command-line interface for Gnu/Linux OS, it is the clone of Unix OS command-line interface a.k.a CLI. It is commonly used by Sysadmins, programmers and it is handy for every Gnu/Linux user.
I first used a Gnu/Linux OS back in 2008 just for fun, however, I was obliged to get back to it as I was not satisfied with WSL of MS Windows 10 to learn and use WP-CLI.
My main OS now is Ubuntu, I use it for my translation work and programming as well. Meantime, on a few occasions, I have to use MS Win 10 if some programs for my language services work best on Windows and it could be counterproductive to use an emulator such as Wine.
If appropriately learned it helps speed up many tasks either with commands or using bash scripts.
Must know and commonly used shortcuts for Bash/terminal
Ctrl + Alt + T | Open terminal |
Shift + Ctrl + T | Open a new tab |
Shift + Ctrl + W | Close current tab |
Ctrl + Page Up | Move to the previous tab |
Ctrl + Page Down | Move to the next tab |
Ctrl + A | Move to the beginning of the line |
Ctrl + E | Move to the end of the line |
Alt + F | Move one word forward |
Alt + B | Move one word backward |
Ctrl + L | Clear the terminal screen |
Ctrl + R | Incremental reverse search of bash history |
Ctrl + U | Deletes before the cursor until the start of the command |
Ctrl + K | Deletes after the cursor until the end of the command |
TAB | Autocomplete the command |
Basic commands
List/display files and directories:
“ls” is used to display a list of files & directories
Use ls -a to display all files.
It marks executable files with an asterisk (*)
You can use the -F parameter with the ls command to easily distinguish files from
directories.
ls -Fa
Option parameters can be written together: ls -F -R . They can
often be combined as follows: ls -FR.
The -R parameter is another option the ls can use.
-R, –recursive list subdirectories recursively
The -l parameter displays a long listing format and provides more information about each file.
If you want to view the long listing for only one file, simply tack on the file’s name to your ls -l command as in the example below.
fakhri@dev:~/Documents$ ls -l Fakhri\ Azzouz\ Linguist\ 2022\ Resume.pdf
-rw-rw-r-- 1 fakhri fakhri 88097 أفريل 27 09:22 'Fakhri Azzouz Linguist 2022 Resume.pdf'
However, if you want to see such a listing for a directory, without its contents, you’ll not only need to add its name to the command but also add the -d switch, as in:
ls -ld Directory_Name
Change directory:
To open a specific directory type “cd” (change directory) then the directory name or its path.
The command below opens the “Documents” directory.
fakhri@dev:~$ cd Documents/
To navigate to your home directory, use “cd” or “cd ~”
“cd ..” To move to the parent directory
Create a directory:
“mkdir” (make directory) creates a new directory named “dir1“.
fakhri@dev:~$ mkdir dir1
Copy:
cp to copy a file to a directory or file onto another file
cp SOURCE DESTINATION
fakhri@dev:~/Documents$ cp testcpdopy.txt ../Desktop
fakhri@dev:~/Documents$ cd ..
fakhri@dev:~$ cd Desktop/
fakhri@dev:~/Desktop$ ls
testcpdopy.txt
fakhri@dev:~/Desktop$
“../Desktop” is typed to both go to the parent directory and then move to the Desktop directory.
“..” moves to the parent directory and “/Desktop” moves to the Desktop directory
“cp” can be used with wildcards such as “*“
fakhri@dev:~/Documents$ touch testfile1 testfile2
fakhri@dev:~/Documents$ ls
'Fakhri Azzouz Linguist 2022 Resume.pdf'
'Gnu Linux training'
'restaurant rent contract 12-22-2021 15.06.pdf'
testfile1
testfile2
fakhri@dev:~/Documents$ cp test* ../Desktop
fakhri@dev:~/Documents$ cd ../Desktop
fakhri@dev:~/Desktop$ ls
testfile1 testfile2
We created 2 files with “touch
testfile1 testfile2
“
Then using the wildcard * we copied them to the Desktop directory
In this case, asterisks * is a wildcard / globbing character that searches for files that start with “test” followed by any other number of characters.
Remove a file(s) or a directory:
“rm” is the command used to remove a file or an empty directory. To remove a directory that contains file(s) use “rm -r” and then the directory name.
fakhri@dev:~/Documents/testdir$ ls
testfile1 testfile2
fakhri@dev:~/Documents/testdir$ cd ..
fakhri@dev:~/Documents$ rm testdir/
rm: cannot remove 'testdir/': Is a directory
fakhri@dev:~/Documents$ rm -r testdir/
fakhri@dev:~/Documents$ ls
You can see in the commands above that when a directory contains a file or files you cannot delete it with “rm” command only as there is an error message that says:
rm: cannot remove 'testdir/': Is a directory
However, when you type rm -r /testdir the command works and successfully deletes the directory and its content.
We can also use wildcards with “rm” as shown below.
fakhri@dev:~/Desktop$ ls
testcopydot testcopydot2 testcpdopy.txt
fakhri@dev:~/Desktop$ rm test*
fakhri@dev:~/Desktop$ ls
Create a file:
touch is typed to create a file or multiple files for example:
fakhri@dev:~/Documents$ touch testfile1 testfile2
The above command creates 2 files: testfile1 testfile2
fakhri@dev:~$ touch exampletext.txt
The above command creates one text file named exampletext.txt
You can also use “cat >filename“ to create a file, when you use this command you will be directed to a new line where you write into the file.
When you finish type ctrl + d to save and exit.
After that please besides, “cat” command prints the content of a file in the standard output.
Move a file or a directory:
mv to move files and directories and also to rename them
mv [OPTIONS] SOURCE DESTINATION
Wildcards globbing:
Wildcards are mainly used to search for file names. A question mark (?) to represent one character, and an asterisk (*) to represent any number of characters.
fakhri@dev:~/Documents$ ls
'25 04 2022 Finance.xhb'
'25 04 2022 Finance.xhb~'
createwithcat2.txt
'Fakhri Azzouz Linguist 2022 Resume.pdf'
filecreatewithcat
filecreatewithcat.txt
'Gnu Linux training'
testfile1
testfile2
fakhri@dev:~/Documents$ ls file*
filecreatewithcat filecreatewithcat.txt
fakhri@dev:~/Documents$ ls testfile?
testfile1 testfile2
Concerning this command:
ls file*
It looks for files that start with “file” and ends with more than one character.
ls testfile?
The command above look for files that start with “testfile” and ends with 1 other character.
and to select a range of characters, like an alphabetic range “[a-j]” :
fakhri@dev:~/Documents$ touch fill fall fell fyll fxll
fakhri@dev:~/Documents$ ls f[a-i]ll
fall fell fill
To specify what should be excluded use “!“:
fakhri@dev:~/Documents$ ls f[!ea]ll
fill fxll fyll
There is so much to cover in Bash, I prefer not to make this article longer and keep it as an introduction to the basics.
More articles are planned in the future, if you have any comments or suggestions let me know in the comments below.