6.4: Linux Commands
- Last updated
- Save as PDF
- Page ID
- 469685
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
Directory (Folder) Commands
cd
cd <path> directs to a specific directory in the tree
note, there must be a space after the cd
cd switches
the following switches work on cd
cd /home/pi cd #by itself takes you home cd ~ navigate to home directory cd / navigate to root directory cd .. go up one level cd - go back one level
cp
cp - copy directory
We can copy a directory to a new location by using the command:
- cp directory_name destination : Copies directory (or file) to a new location
Note
To include the subfolders of a directory, we need to add -r (recursive)
cp /tmp/test/subdir2 /tmp/test/subdir1 -r cd subdir1 ls
- Output:
Relative path
The previous examples that we worked on used some relative paths. Working with a relative path means that the place where you go depends on your current working directory.
The "etc" directory is a file that is located directly inside the root of the file system. If you were to try and cd to the "etc" folder while you are in the root directory, it will work.
cd / pwd cd etc pwd
However, if you are not in your root directory, that won't work.
cd pwd cd etc pwd
You will get an error saying "No such file or directory".
Changing the working directory will affect where you start. The path of the directory you want to access only makes sense while it is relative to your working directory.
Absolute path
Absolute commands have the same effect no matter what your current working directory is. From our previous examples, the cd command is an absolute one. When you run it on its own, you go straight to your home directory. Another previous example is the cd / command. It directly switches you to the root directory when you run it on its own.
Note: Any path starting with / is an absolute path. When you want to switch to a directory and start your path with the /, it is the equivalent of "going to the root directory and then to the folder that comes after the slash".
cd pwd cd /etc pwd
find
find works on both directories and files.
ls
With the last command, we created two new subdirectories. We can look at the list with the command:
- ls : list
ls
- Output:
ls switches
ls ls /home/pi ls -1 (one, to make a single column) ls -l (long, to list info) ls -l -a (long and all, to include hidden files ls ../ (reaches up one level)
mkdir
the following commands show you where you are, make a new directory, show you the new directory and move you to the new directory
pwd mkdir new_directory ls cd new_directory ls
rm - Remove Directory
for empty directories
rm switches
rm -d directory_name rm -r recursive and removes subfolders rm -rf recursive and forced
for directories with files in them (-r is recursive, and removes contents of directory)
rm -r directory-name
The -r is a switch making the command recursive
to do it without asking for confirmation
rm -rf directory_name
The -f switch "forces" the action
BE VERY CAREFUL WHEN USING rm -r or rm -rf as you delete everything, including subfolders.
pwd
Identifies Current Directory
rebelford@raspberrypi:~ $ pwd
pwd
- Output:
tree
tree
This shows the file structure from where you are
File Commands
cat
We can view the content of the file on the terminal by using the command:
The following command allows you to see what your operating system is
- cat file _name: Displays content of a file (concatenate)
cat ls_output.txt
cat does not support keyboard scrolling
cat /etc/os-release
echo
We can display some text that we type into the command line, not necessarily reading it from a file, by using the command:
- echo "text": printing text on the terminal
echo "display new text"
The echo command prints its parameters
echo can create a new file when used with a redirect and place its content in the file
echo "insert this text into new file" > newfile.txt
find
find a file in a directory
note the "i" in iname makes it case insensitve
find /home/path -iname filename.extension find /home/path -filename *.extension
grep
To find a string in a file, we use the command:
- grep string_content file_name : Global Regular Expression Print
head
To display the beginning of a file, we use the command:
- head file_name : Displays the beginning (head) of a file
head -n1 ls_output.txt (displays first 1 line of ls_output.txt) head -n2 ls_output.txt (displays first 2 lines of ls_output.txt)
less
more
displays content of a file one page at a time
mv
Let's move one of the test files that we created previously to one of our subdirectories. This is done b the command:
- mv file_name directory_name : move a file to a directory
rm
- rm file_name : remove
rm test_file_2.txt
sed
sed- Stream editor
sed [intial word/new word] filename
For command testing purposes, let's create a new file with the following text:
echo "Course number 1, course number 2, course number 3, course number 4" > test_file_3.txt
To find and replace something in a file, we can use the command:
sed 's/course/class/' test_file_3.txt
If we now check the content of the file using cat:
cat test_file_3.txt
- Output:
-
Every word "course" is replaced by the word "class":
"Class number 1, class number 2, class number 3, class number 4"
screenshot to be added
sort
sort file_to_sort.txt
sort switches
Sorting for Column Number
Sorting can be done with file content that has more than one column. Let's create a new text file entitled "file_to_sort_columns.txt" in which there are names and ages (2 columns).
Let's organize them in ascending order. For that, we use the keyword –k in the command and –n for numerical sorting (Since there are two columns, 2 is used with -n)
sort –k 2n file_to_sort_columns.txt
- Output:
-
to be added
Checking the Sorting of the file
If we want to check if the file was sorted, we can display the file content using the cat command:
cat file_to_sort_columns.txt
- Output:
-
to be added
Removing duplicates
If there are repeated words in the file, we can use the sort command with -u option to remove the duplicate, as follows:
sort -u file_to_sort_columns.txt
- Now, we can see that the data is sorted and the repeated items are removed from the output.
-
to be added
Sorting in Reverse Order
We can sort the file in reverse order (descending order by default) sort by using -R option with the sort command.
sort -R file_to_sort_columns.txt
-
Output:
to be added
tail
Shows end of file
touch
- used to create any type of file with zero size
touch demo.txt
The creation of files can be done in different ways, one of them is using redirection.
a
wc
word count
wc switches
wc -l line count
User Commands
adduser
sudo adduser the-name-of-user-you-want
password is optional
the new user will be in the home directory
chmod
this command changes the permissions of types of users with regards to access to files and folders
For the use of this command, three things need to be set, who you are setting the permission for, how you are setting it and to what are you setting it
Who we are setting permissions to:
u : owner of the file
g : the file group
o : everyone who is not an owner
a : everyone
How we are setting the permissions:
+ : turns on a permission
- : turns off a permission
= : ignores current permissions and sets new on
What we are setting them to:
r : read
w : write
x : execute
X : special execute for folders
Now let's grant execute permissions to the user of a fictitious file.
note, you may need to use sudo (if you are not the owner of the file)
chmod u+x some_file.txt
passwd
allows you to change your password
a user with sudo permission can change a different user's password
Operating System Commands
date
date date +%m-%d-%y
df - free space
Display Partitions (free space)
To make the output easier to understand (in Kilobytes, Megabytes and Gigabytes), use the option -h (human-readable) with the df command
To know the storage usage of each folder, use the command:
df df -h
- Output:
du - disk usage
To know how much storage has been used from the disk, use the command:
- du : disk usage
-
du du/home/pi
for a specific directory (home/pi)
Be careful!
By using the du command by itself, be ready to get a big number of logs with all the files that exist on your disk!
We will be discussing the options to get precise outputs using the du command in the following part.
To know the storage usage of each folder, use the command:
du -h --max-depth=1 ./
- Output:
dmesg
displays kernel related messages about hardware, device drivers, initialization and bootup issues
In this activity we will check bootup processes and errors
dmesg
- Output:
free
To know how much RAM the server has, use the command:
- free
- free -b : b is for bytes
- free -k : k is for kilo-bytes
- free -m : m is for mega-bytes
- free -g : g is for giga_bytes
free free -b
- Output:
htop
Monitor running processes
In this activity we will check bootup processes and errors (you need to use ctrl c to terminate
htop
- Output:
kill/killall
kills a specific process by its process id, which you get with the ps command
ps kill ###(the process ID of the process you want to kill)
lscpu
list number of cpus
lscpu
- Output:
ps
To display the currently running processes, use the command:
- ps : process status: produces a snapshot of the running processes.
ps
- Output:
-
The output contains a list of the running processes under 4 columns:
- PID: the process identification number
- TTY: the terminal name
- TIME: the running time
- CMD: the name of the command that launches the process
Options that can be used with the ps command:
- ps -a : lists all the running processes of all the users
- ps -u : lists additional information (memory usage, CPU usage percentage, process state code, and process owner)
reboot
sudo reboot
shutdown
sudo shutdown -h now sudo shutdown -h 20:00
-h switch halts processes, and the 20:00 specifies the time for shutdown
top
resource-usage of processes
top
- Output:
Note
Unlike the ps command, the top command output updates periodically; You will see real-time updates for running times and CPU usage.
The output of the top command is a shell that allows the user to move through processes and interact with them.
Interacting with a process is done by the keys:
- k : kills the process
- M : sorts the list by memory usage
- N : sorts the list by the process identification numbers
- r : changes the priority of the process
- d : changes the refresh time interval
- c : displays the path of the process
These are not normal Bash commands, but commands in the RPi OS that specifically deal with functionality of the RPi Chip and hardware. Note, some of these vary across different version of the RPi OS.
Raspberry Pi OS Commands
libcamera-still
libcamera-vid
raspi-config
sudo raspi-config
raspi-gpio
vcgencmd measure_temp
measures temperature of Raspberry Pi
General Commands
clear
clears the terminal
history
!history number runs that command in history
man
this is the help manual. the following gets you the manual for the wget command
wget man
TAR
create archive
tar -cvfz archive.tar.gz/home/pi/folder
extract files
tar -xvfz archive.tar.gz
c - to compress
x - to extract
v - verbose
f - specify filename
z - use gZip to compress
zip/unzip
similar to tar but used on Windows systems
create zip
zip -r archive.zop /home/pi/folder-to-zip
extract zip
unzip file.zip
Network Commands
ifconfig
interface configuration note ipconfig is a similar windows command
iwconfig
wireless network configuration info
netstat
allows you to monitor network status for all ports
netstat netstat -l
ping
checks to see if host is alive
ping ipadress
wget
download a file from the terminal
wget https://file_url.extension
ssh
connects to another Linux system using SSH
ssh user@IP
scp
copies files using SSH
Package Management
apt update
downloads latest package list from repository
sudo apt update
apt upgrade
downloads and installs packages from repository
sudo apt upgrade
dpkg
lists installed pacakges
dpkg -l
apt install
installs specified package
sudo apt instal name-of-package
apt search
searches for named package
apt search name-of-package
apt remove
removes named package
sudo apt remove name-of-package