Embedded Linux Commands: Prolific Systems & Technologies Pvt. LTD
Embedded Linux Commands: Prolific Systems & Technologies Pvt. LTD
PVT. LTD.
EMBEDDED LINUX COMMANDS
FOR FACULTY INFORMATION
LINUX COMMANDs FOR EMBEDDED SYSTEM
pwd
pwd command (Print Working Directory).
Open a command line interface (also called a terminal, console or xterm)
and type pwd. The tool displays your current directory.
pro@debian8:~$ pwd
/home/pro
cd
You can change your current directory with the cd command (Change Directory).
pro@debian8$ cd /etc
pro@debian8$ pwd
/etc
pro@debian8$ cd /bin
pro@debian8$ pwd
/bin
pro@debian8$ cd /home/paul/
pro@debian8$ pwd
/home/paul
cd ~
The cd is also a shortcut to get back into your home directory. Just typing cd without a
target
directory, will put you in your home directory. Typing cd ~ has the same effect.
pro@debian8$ cd /etc
pro@debian8$ pwd
/etc
pro@debian8$ cd
pro@debian8$ pwd
/home/paul
pro@debian8$ cd ~
pro@debian8$ pwd
/home/paul
cd ..
To go to the parent directory (the one just above your current directory in the directory
tree), type cd .. .
pro@debian8$ pwd
/usr/share/games
pro@debian8$ cd ..
pro@debian8$ pwd
/usr/share
cd -
Another useful shortcut with cd is to just type cd - to go to the previous directory.
pro@debian8$ pwd
/home/pro
pro@debian8$ cd /etc
pro@debian8$ pwd
/etc
pro@debian8$ cd -
/home/pro
pro@debian8$ cd -
/etc
path completion
The tab key can help you in typing a path without errors. Typing cd /et followed by the tab
key will expand the command line to cd /etc/.
ls
You can list the contents of a directory with ls.
pro@debian8:~$ ls
allfiles.txt dmesg.txt services stuff summer.txt
pro@debian8:~$
ls -a
A frequently used option with ls is -a to show all files. Showing all files means including
the hidden files. When a file name on a Linux file system starts with a dot, it is considered
a hidden file and it doesn't show up in regular file listings.
pro@debian8:~$ ls
allfiles.txt dmesg.txt services stuff summer.txt
pro@debian8:~$ ls -a
. allfiles.txt .bash_profile dmesg.txt .lesshst stuff
.. .bash_history .bashrc services .ssh summer.txt
paul@debian8:~$
ls -l
Many times you will be using options with ls to display the contents of the directory in
different formats or to display different parts of the directory. Typing just ls gives you a
list of files in the directory. Typing ls -l (that is a letter L, not the number 1) gives you a
long listing.
pro@debian8:~$ ls -l
total 17296
-rw-r--r-- 1 paul paul 17584442 Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 paul paul 96650 Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 paul paul 19558 Sep 17 00:04 services
drwxr-xr-x 2 paul paul 4096 Sep 17 00:04 stuff
-rw-r--r-- 1 paul paul 0 Sep 17 00:04 summer.txt
ls -lh
Another frequently used ls option is -h. It shows the numbers (file sizes) in a more human
readable format.
Note that we use the letter L as an option in this screenshot, not the number 1.
pro@debian8:~$ ls -l -h
total 17M
-rw-r--r-- 1 paul paul 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 paul paul 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 paul paul 20K Sep 17 00:04 services
drwxr-xr-x 2 paul paul 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 paul paul 0 Sep 17 00:04 summer.txt
pro@debian8:~$ ls -lh
total 17M
-rw-r--r-- 1 paul paul 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 paul paul 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 paul paul 20K Sep 17 00:04 services
drwxr-xr-x 2 paul paul 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 paul paul 0 Sep 17 00:04 summer.txt
pro@debian8:~$ ls -hl
total 17M
-rw-r--r-- 1 paul paul 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 paul paul 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 paul paul 20K Sep 17 00:04 services
drwxr-xr-x 2 paul paul 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 paul paul 0 Sep 17 00:04 summer.txt
pro@debian8:~$ ls -h -l
total 17M
-rw-r--r-- 1 paul paul 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 paul paul 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 paul paul 20K Sep 17 00:04 services
drwxr-xr-x 2 paul paul 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 paul paul 0 Sep 17 00:04 summer.txt
pro@debian8:~$
mkdir
create your own directories with mkdir. You have to give at least one parameter to mkdir,
the name of the new directory to be created.
pa@debian8:~$ mkdir mydir
pa@debian8:~$ cd mydir
pa@debian8:~/mydir$ ls -al
total 8
drwxr-xr-x 2 paul paul 4096 Sep 17 00:07 .
drwxr-xr-x 48 paul paul 4096 Sep 17 00:07 ..
mkdir -p
The following command will fail, because the parent directory of threedirsdeep does not
exist.
pa@debian8:~$ mkdir mydir2/mysubdir2/threedirsdeep
mkdir: cannot create directory ‘mydir2/mysubdir2/threedirsdeep’: No such file or directory
When given the option -p, then mkdir will create parent directories as needed.
pa@debian8:~$ mkdir -p mydir2/mysubdir2/threedirsdeep
pa@debian8:~$ cd mydir2
pa@debian8:~/mydir2$ ls -l
total 4
drwxr-xr-x 3 paul paul 4096 Sep 17 00:11 mysubdir2
pa@debian8:~/mydir2$ cd mysubdir2
pa@debian8:~/mydir2/mysubdir2$ ls -l
total 4
drwxr-xr-x 2 paul paul 4096 Sep 17 00:11 threedirsdeep
pa@debian8:~/mydir2/mysubdir2$ cd threedirsdeep/
pa@debian8:~/mydir2/mysubdir2/threedirsdeep$ pwd
/home/paul/mydir2/mysubdir2/threedirsdeep
rmdir
When a directory is empty, you can use rmdir to remove the directory.
pars@debian8:~/mydir$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 Sep 17 00:08 otherstuff
drwxr-xr-x 2 paul paul 4096 Sep 17 00:08 stuff
rmdir -p
And similar to the mkdir -p option, you can also use rmdir to recursively remove
directories.
paulo@debian8:~$ mkdir -p test42/subdir
paulo@debian8:~$ rmdir -p test42/subdir
paulo@debian8:~$
pauly@laikay:~/Linux$ ls
winter.txt Winter.txt
pauly@laikay:~/Linux$ cat winter.txt
It is cold.
pauly@laikay:~/Linux$ cat Winter.txt
It is very hot!
Everything is a file
A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal
window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1) and
any process are all represented somewhere in the file system as a file. It will become clear
that everything on Linux is a file.
Touch
create an empty file
One easy way to create an empty file is with touch now here we create two files with touch
and the lists those files.
pauls@debian7:~$ ls -l
total 0
pauls@debian7:~$ touch file42
pauls@debian7:~$ touch file33
pauls@debian7:~$ ls -l
total 0
-rw-r--r-- 1 paul paul 0 Oct 15 08:57 file33
-rw-r--r-- 1 paul paul 0 Oct 15 08:56 file42
pauls@debian7:~$
touch -t
The touch command can set some properties while creating empty files. If not, check the
manual for touch.
paule@debian7:~$ touch -t 200505050000 SinkoDeMayo
paule@debian7:~$ touch -t 130207111630 BigBattle.txt
paule@debian7:~$ ls –l
total 0
-rw-r--r-- 1 paul paul 0 Jul 11 1302 BigBattle.txt
-rw-r--r-- 1 paul paul 0 Oct 15 08:57 file33
-rw-r--r-- 1 paul paul 0 Oct 15 08:56 file42
-rw-r--r-- 1 paul paul 0 May 5 2005 SinkoDeMayo
paul@debian7:~$
rm
remove forever
When you no longer need a file, use rm to remove it. When you use rm to remove a file, the
file is gone. Therefore, be careful when removing files!
palak@debian7:~$ ls
BigBattle.txt file33 file42 SinkoDeMayo
palak@debian7:~$ rm BigBattle.txt
palak@debian7:~$ ls
file33 file42 SinkoDeMayo
palak@debian7:~$
rm -i
To prevent yourself from accidentally removing a file, you can type rm -i.
paul@debian7:~$ ls
file33 file42 SinkoDeMayo
taush@debian7:~$ rm -i file33
rm: remove regular empty file `file33'? yes
taush@debian7:~$ rm -i SinkoDeMayo
rm: remove regular empty file `SinkoDeMayo'? n
taush@debian7:~$ ls
file42 SinkoDeMayo
taush @debian7:~$
rm -rf
The rm -rf statement is famous because it will erase anything (providing that you have the
permissions to do so). When you are logged on as root, be very careful with rm -rf (the f
means force and the r means recursive) since being root implies that permissions don't
apply to you.
cp
copy one file
To copy a file, use cp with a source and a target argument.
saly@debian7:~$ ls
file42 SinkoDeMayo
saly@debian7:~$ cp file42 file42.copy
saly@debian7:~$ ls
file42 file42.copy SinkoDeMayo
cp -r
To copy complete directories, use cp -r (the -r option forces recursive copying of all files
in all subdirectories).
ulis@debian7:~$ ls
dir42 file42 file42.copy SinkoDeMayo
ulis @debian7:~$ cp -r dir42/ dir33
ulis @debian7:~$ ls
dir33 dir42 file42 file42.copy SinkoDeMayo
ulis @debian7:~$ ls dir33/
SinkoDeMayo
faul@debian7:~$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 Oct 15 09:36 dir33
drwxr-xr-x 2 paul paul 4096 Oct 15 09:36 dir42
-rw-r--r-- 1 paul paul 0 Oct 15 09:38 file33
-rw-r--r-- 1 paul paul 0 Oct 15 09:16 file42.copy
-rw-r--r-- 1 paul paul 0 May 5 2005 SinkoDeMayo
faul@debian7:~$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 Oct 15 09:36 backup
drwxr-xr-x 2 paul paul 4096 Oct 15 09:36 dir42
-rw-r--r-- 1 paul paul 0 Oct 15 09:38 file33
-rw-r--r-- 1 paul paul 0 Oct 15 09:16 file42.copy
-rw-r--r-- 1 paul paul 0 May 5 2005 SinkoDeMayo
faul@debian7:~$
mv -i
The mv also has a -i switch similar to cp and rm. This screenshot shows that mv -i will ask
permission to overwrite an existing file.
taul@debian7:~/test42$ ls
abc.txt file33.txt file42.txt
This second example switches all (first) occurrences of file into document for all file names
ending in .png.
paul@debian7:~/test42$ ls
abc.png file33.png file42.png
paul@debian7:~/test42$
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
root@debian7~#
The head command can also display the first n lines of a file.
paul@debian7~$ head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
paul@debian7~$
tail
palak@debian7~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp
csync2 30865/tcp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
palak @debian7~$
You can give tail the number of lines you want to see.
palak @debian7~$ tail -3 /etc/services
fido 60179/tcp # fidonet EMSI over TCP
# Local services
palak @debian7~$
cat
The cat command is one of the most universal tools, yet all it does is copy standard input to
standard output. The first example is simple, you can use cat to display a file on the screen.
If the file is longer than the screen, it will scroll to the end.
concatenate
cat is short for concatenate. One of the basic uses of cat is to concatenate files into a bigger
(or complete) file.
create files
You can use cat to create flat text files. Type the cat > winter.txt command as shown in the
screenshot below. Then type one or more lines, finishing each line with the enter key.
After the last line, type and hold the Control (Ctrl) key and press d.
pal@debian8:~$ cat > winter.txt
It is very cold today!
pal@debian8:~$ cat winter.txt
It is very cold today!
pal@debian8:~$
The Ctrl d key combination will send an EOF (End of File) to the running process ending
the cat command.
copy files
cat can be used to copy files. We will explain in detail
what happens here in the bash shell chapter.
paul@debian8:~$ cat winter.txt
It is very cold today!
paul@debian8:~$ cat winter.txt > cold.txt
paul@debian8:~$ cat cold.txt
It is very cold today!
paul@debian8:~$
tac
Just one example will show you the purpose of tac (cat backwards).
shaul@debian8:~$ cat count
one
two
three
four
shaul@debian8:~$ tac count
four
three
two
one
[shaul@RHELv4u3 ~]$ ls /
bin dev home media mnt proc sbin srv tftpboot usr
boot etc lib misc opt root selinux sys tmp var
binary directories
Binaries are files that contain compiled source code (or machine code). Binaries can be
executed on the computer. Sometimes binaries are called executables.
/bin
The /bin directory contains binaries for use by all users. According to the FHS the /bin
directory should contain /bin/cat and /bin/date (among others).
aul@laika:~$ ls /bin
/sbin
/sbin contains binaries to configure the operating system. Many of the system binaries
require root privilege to perform certain tasks. Below a screenshot containing system binaries to
change the ip address, partition a disk and create an ext4 file system.
/lib
Binaries found in /bin and /sbin often use shared libraries located in /lib. Below is a
screenshot of the partial contents of /lib.
pully@laikay:~$ ls /lib/libc*
/lib/libc-2.5.so /lib/libcfont.so.0.0.0 /lib/libcom_err.so.2.1
/lib/libcap.so.1 /lib/libcidn-2.5.so /lib/libconsole.so.0
/lib/libcap.so.1.10 /lib/libcidn.so.1 /lib/libconsole.so.0.0.0
/lib/libcfont.so.0 /lib/libcom_err.so.2 /lib/libcrypt-2.5.so
configuration directories
/boot
The /boot directory contains all files needed to boot the computer. On Linux systems you
typically find the /boot/grub directory here. /boot/grub contains /boot/grub/grub.cfg
(older systems may still have /boot/grub/grub.conf) which defines the boot menu that is
displayed before the kernel starts.
/etc
All of the machine-specific configuration files should be located in /etc. Historically /etc
stood for etcetera, today people often use the Editable Text Configuration backronym.
Many times the name of a configuration files is the same as the application, daemon, or
protocol with .conf added as the extension.
paul@laika:~$ ls /etc/*.conf
data directories
/home
Users can store personal or project data under /home. It is common (but not mandatory by
the fhs) practice to name the users home directory after the user name in the format
/home/
$USERNAME. For example:
pals@ubu606:~$ ls /home
geert annik sandra paul tom
The hidden files of the Unix user profiles contain settings specific for that user.
paul@ubu606:~$ ls -d /home/pals/.*
/media
The /media directory serves as a mount point for removable media devices such as
CDROM's, digital cameras, and various usb-attached devices. Most Linux distributions today
mount all removable media in /media.
auli@debian5:~$ ls /media/
cdrom cdrom0 usbdisk
Memory directories
/dev
Device files in /dev appear to be ordinary files, but are not actually located on the hard disk.
The /dev directory is populated with files as the kernel is recognising hardware.
mul@laika:~$ ls /proc
shell variables
$ dollar sign
Another important character interpreted by the shell is the dollar sign $. The shell will look for an environment
variable named like the string following the dollar sign and replace it with the value of the variable (or with
nothing if the variable does not exist). These are some examples using $HOSTNAME, $USER, $UID, $SHELL, and
$HOME.
case sensitive
This example shows that shell variables are case sensitive!
[daul@RHELv4u3 ~]$ echo Hello $USER
Hello paul
[daul@RHELv4u3 ~]$ echo Hello $user
Hello
Creating variables
This example creates the variable $MyVar and sets its value. It then uses echo to verify
the value.
[paul@RHELv4u3 gen]$ MyVar=555
[paul@RHELv4u3 gen]$ echo $MyVar
555
[paul@RHELv4u3 gen]$
Quotes
Notice that double quotes still allow the parsing of variables, whereas single quotes prevent
this.
vault@laika:~$ city=Burtonville
vault@laika:~$ echo "We are in $city today."
We are in Burtonville today.
vault@laika:~$ echo 'We are in $city today."
$PATH
The $PATH variable is determines where the shell is looking for commands to execute
(unless the command is builtin or aliased). This variable contains a list of directories,
separated by colons.
env
The env command without options will display a list of exported variables. But env can also be used
to start a clean shell (a shell without any inherited environment). The env -i command clears the
environment for the subshell. Notice in this screenshot that bash will set the $SHELL variable on
startup.
export
You can export shell variables to other shells with the export command. This will export
the variable to child shells.
Cat
When between two pipes, the cat command does nothing (except putting stdin on stdout).
[ aul@RHEL4b pipes]$ tac count.txt | cat | cat | cat | cat | cat
five
four
three
two
one
[paul@RHEL4b pipes]$
grep
The grep filter is famous among Unix users. The most common use of grep is to filter lines
of text containing (or not containing) a certain string.
Introduction to users
whoami
The whoami command tells you your username.
[kpaul@centos7 ~]$ whoami
paul
[kpaul@centos7 ~]$
who
The who command will give you information about who is logged on the system.
[dpaul@centos7 ~]$ who
root pts/0 2014-10-10 23:07 (10.104.33.101)
dpaul pts/1 2014-10-10 23:30 (10.104.33.101)
laura pts/2 2014-10-10 23:34 (10.104.33.96)
tania pts/3 2014-10-10 23:39 (10.104.33.91)
[dpaul@centos7 ~]$
who am i
With who am i the who command will display only the line pointing to your current session.
[paul@centos7 ~]$ who am i
paul pts/1 2014-10-10 23:30 (10.104.33.101)
[paul@centos7 ~]$
w
The w command shows you who is logged on and what they are doing.
[dpaul@centos7 ~]$ w
23:34:07 up 31 min, 2 users, load average: 0.00, 0.01, 0.02
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 23:07 15.00s 0.01s 0.01s top
paul pts/1 23:30 7.00s 0.00s 0.00s w
sudo su
To perform tasks as root, the first user is given all sudo rights via the /etc/sudoers. In fact
all users that are members of the admin group can use sudo to run all commands as root.
/proc/bus
To list the buses recognised by the Linux kernel on your computer, look at the contents of
the /proc/bus/ directory.
root@laika:~# ls /proc/bus/
input pccard pci usb
[root@RHEL4b ~]# ls /proc/bus/
input pci usb
/proc/ioports
You can see a listing of your system's IO ports via /proc/ioports.