In this article, we will introduce some basic commands that we need to know when programming with bash script. Practice fluently all this commands makes us to be possible to write scripting in a large project.
Let’s get started.
Table of contents
- Open command line interface
- Understanding about user’s level in Linux
- Running bash file
- Comment something
- Commands with File system
- Redirecting output
- Some useful command-line tricks
- Wrapping up
Open command line interface
First way, click Terminal icon that is at the bottom of sidebar.
Second way, use shorcut key
Ctrl + Alt + T.
Understanding about user’s level in Linux
Normal users has limited permission such as they do not install software, …
The purpose of
sudois to enable us to use our user account to do things that normally only
rootwould have been able to do.
When we use
sudo, we’ll be aksed for our user’password for confirmation, and then the command will execute.
rootuser account exists on all Linux distributions and is the most powerful user account on the planet.
rootuser account can be used to do anything:
- create files or directories.
- install software.
- destroy entire installation with one typo or ill-conceived command.
- delete all files on hard disk.
–> There is often not so much as a confirmation prompt while executing any commands as
It’s for these reasons that it’s generally recommended that we should not use
rootaccount unless we absolute have to.
Running bash file
Ctrl + Alt + T
In Terminal, use nano editor to create the file and file’s name.
In bash fist.sh, we type our commands such as:
#!/bin/bash echo "Hello, world!"
First way, using bash command
Second way, setting execute permission to bash file and run this file.
chmod a+x firsh.sh ./first.sh
#!/bin/bash # Use "#" symbol to create single line comment. echo "single line comment"
#!/bin/bash :' Using multi-line comment in this file testfile.sh ' echo 'hello, world'
Commands with File system
List all files in directory
ls # or dir
rmcommand to remove a file with specific path.
rm [-f|i|I|q|R|r|v] file
deletes read-only files immediately without any confirmation.
prompts for confirmation before deleting every files before entering a sub-directory if used with
suppresses all the warning messages, however error messages are still displayed.
delete recursively files and folders in parent folder.
as same as
display the file names on the output.
prompts everytime when an attempt is made to delete for than 3 files at a time or while removing recursively.
If we have a file with
mistake-test.txt, we want to rename this file to
mv mistake-text.txt test.txt
List all installed packages
sudo apt list --installed # or dpkg --get-selections > list.txt # list a specific installed package dpkg --get-selections | grep postgres
Set PATH for JDK
First, we can install our latest jdk.
sudo apt install default-jdk sudo apt install default-jre
If we encounter the problem such as
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable), we can solve the problem with https://askubuntu.com/questions/1109982/e-could-not-get-lock-var-lib-dpkg-lock-frontend-open-11-resource-temporari.
Then, we need to find our jdk path.
# update databse of ubuntu sudo updatedb # determine whether jdk is installed locate openjdk
So, we have:
If most of the return outputs are “/usr/lib/jvm/java-11-openjdk-amd64”, we would use this path to set the Java_Home path.
Check all version of JDK in our computer
sudo update-java-alternatives --list
.bashrcfile is a hidden file inside our home directory.
ls -la ~/ | more # Then, use cd command to go to our user directory cd <user_name> # Finally, use nano to open .bashrc file nano .bashrc
Add the following commands to
export JAVA_HOME="usr/lib/jvm/java-11-openjdk-amd64" export PATH=$PATH:$JAVA_HOME/bin
Now, apply all the changes into current running system.
Check current JDK’s version
# download netbeans cd /tmp && wget -c http://download.netbeans.org/netbeans/8.2/final/bundles/netbeans-8.2-linux.sh # run netbeans chmod +x netbeans-8.2-linux.sh sudo ./netbeans-8.2-linux.sh
# refreshing the repositories sudo apt update # its wise to keep the system up to date! # you can skip the following line if you not # want to update all your software sudo apt upgrade # installing python 2.7 and pip for it sudo apt install python2.7 python-pip # installing python-pip for 3.6 sudo apt install python3-pip # NOTE: Do not try to remove python 3.6 as it will screw up your system
sudo apt update sudo apt install git # check git's version git --version
List Ubuntu service with systemctl
Services are managed by
systemdsince Ubuntu 15. In order to list all services, use
systemctl list-units -a # list inactive unit systemctl list-units -a --state=inactive
List Ubuntu services with service command
service --status-all # or service --status-all | grep '\[ + \]' # or service --status-all | grep '\[ - \]'
Remove services that have failed status
systemctl reset-failed # or specific units systemctl reset-failed kafka.service zookeeper.service # other ways systemctl stop [service_name] systemctl disable [service_name] rm /etc/systemd/system/[service_name] rm /etc/systemd/system/[service_name] symlinks that might be related systemctl daemon-reload
# start sudo systemctl start application.service # stop sudo systemctl stop application.service
sudo systemctl restart application.service sudo systemctl reload application.service sudo systemctl reload-or-restart application.service
Starting automatically service
sudo systemctl enable application.service
This will create a
symbolic linkfrom the system’s copy of the service file (usually in
/etc/systemd/system) into the location on disk where
systemdlooks for autostart files (usually
To disable the service from starting automatically, you can type:
sudo systemctl disable application.service
This will remove the symbolic link that indicated that the service should be started automatically.
Check whether process is running on specific port
# 1st - install net-tools package sudo apt-get install net-tools sudo netstat --tulpen | grep 2181 # 2nd - if we do not have permission, we can check from other computer telnet target_ip_addr port
# 1st way - shutdown immediately system sudo power off # 2nd way shutdown -h now # 3rd way - shutdown that asks for a password sudo poweroff # 4th way - shutdown after n minutes shutdown -h <time_in_minutes> shutdown +<time_in_minutes> # Cancel shutdown shutdown -c # shutdown at the specific time # Ex: shutdown -h 20:3 shutdown -h HH:MM
Search for patterns in each file
grep "something" file
With some options:
-i: search for a string case-insensitively in the given file.
-c: find the number of lines that matches the given string/pattern with the -c (count) flag.
-v: invert match - find all lines that do not match.
-r: recursive - search all the files in a directory.
-l: only show the file names of the files that matched.
-f: do not treat the match string as a regex.
-o: only print the matching part of the line (not the whole line).
-a: search binaries - treat binary data like it’s text instead of ignoring it.
It is used to take the output of one command and redirect it into the input of another command.
Output to other commands
cat /var/log/syslog | grep apache2
The above code mean that
catcommand is used to print the contents of the system log stored at
/var/log/syslog. Using the pipe symbol
|, we’re redirecting the output of that command and feeding it into
grep, which is looking for the string
When used together, we’re able to fetch lines from the system log that reference
apache2. This is useful if we’re troubleshouting some sort of issue with the
apacheservice, and we’d like to look at the log file but ignore any lines that aren’t relevant to our investigation.
Output to text file
echo "Hello, world!" >> ~/test-file.txt
echo command normally outputs to the Terminal, but instead telling it to direct its output to a file named
test-file.txt, stored in our home directory.
>>that is used in the above command means that it will continue to append this string to the file.
>is used in the above command, it will actually wipe out the content of
Output errors into Standard Error file descriptor
Normally, we’re working with a File Descriptor called Standard Output - stdout with commands that produce output. File descriptors are numbers that refer to open files, and these numbers are integers.
Standard output is a special file descriptor, with an integer of
1. It is our Terminal.
Standard error - stderr also is file descriptor to be produce in error, with an integer of
find /etc -name *apache* 2>/dev/null
It means that we’re redirecting output from the find command with
2>, instead of
/dev/nullis a special device that anything enters it is never seen or hear from again.
Some useful command-line tricks
Repeat our command that we last used.
As same as
two exclamation marks, we can press the up arrow key and press Enter to recall the previous commmand and execute it.
In order to run previous command with sudo privilege, we can type in terminal like below:
Search in terminal
Ctrl + Rshortcut key to search our previous command.
Edit a command that we’ve previously typed in a text editor.
Assume we pressed the up arrow key, we have a very long command, and we just want to edit part of it without having to execute the entire thing, perhaps a command like this:
sudo apt update && sudo apt install apache2
Let’s assume you want to install
apache2, but the rest of the command is right. If you hold
Ctrland then press
E, which will open the command in a text editor. There, you can change the command. Once you’re done making your changes, the command will execute once you save the file. Admittedly, this is usually only useful when you have a very long command and you need to just change part of it.
We can use the double ampersand - AND operator to do this.
sudo apt update && sudo apt install apache2
It means that the second command will run if the first was successful.
Another way to chain commands is to use semicolon.
sudo apt update; sudo apt install apache2
With using semicolon, we will execute the second command regardless of whether the fist command was successful.
It allows us to create a command that is just another name for another command. This allows us to simplify commands down to just one word or a few letters.
alias install="sudo apt install" // or alias i="sudo apt install" // View the top ten CPU consuming processes alias cpu10='ps auxf | sort -nr -k 3 | head -10' // View the top ten RAM consuming processes alias mem10='ps auxf | sort -nr -k 4 | head -10' // View all amounted filesystems, and present the information in a clean tabbed layout alias lsmount='mount | column -t' // Clear the screen by simply typing c alias c=clear
In the fact, when we exit Terminal window, our aliases are wiped out. In order to retain them, we need to edit our
.bashrcfile. This file is present in our home directory and is read every time we start a new Terminal session.
Change our working directory back to the previous directory we were in
- Normally, we should use confidently some basic commands such as file system, …
- Remember to use some command-line tricks efficiently.