Practical 4 Linux Commands: Working with Directories 1. pwd: pwd stands for Print Working Directory. As the name states, command pwd prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell bash, Bourne shell, ksh, zsh, etc. Simply type pwd in the terminal and press enter, you can see your present working directory as shown below:
2. Cd The cd command, which stands for "change directory", changes the shell's current working directory. The cd command is one of the commands you will use the most at the command line in linux. It allows you to change your working directory. You use it to move around within the hierarchy of your file system. Any directory which is contained inside another directory is called a subdirectory. cd syntax $ cd [directory] cd command examples Change to home directory (determined by $HOME environment variable): $ cd Also change to home directory: $ cd ~ Change to root directory: $ cd / Change to parent directory: $ cd.. Change to subdirectory Documents: $ cd Documents Change to subdirectory Documents/Books: $ cd Documents/Books
Change to directory with absolute path /home/user/desktop: $ cd /home/user/desktop 3. Ls List Files using ls with no option ls with no option list files and directories in bare format where we won t be able to view details like file types, size, modified date and time, permission and links etc.
# ls 0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates anaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos List Files With option l Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and it s permission. # ls -l total 176 -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos View Hidden Files List all files including hidden file starting with.. # ls -a..bashrc Documents.gconfd install.log.nautilus.pulse-cookie...cache Downloads.gnome2 install.log.syslog.netstat.swp.recentlyused.xbel 0001.pcap.config.elinks.gnome2_private.kde.opera.spice-vdagent anaconda-ks.cfg.cshrc.esd_auth.gtk-bookmarks.libreoffice Pictures.tcshrc.bash_history.dbus.fbcmd.gvfs.local.pki Templates.bash_logout Desktop fbcmd_update.php.iceauthority.mozilla Public Videos.bash_profile.digrc.gconf index.html Music.pulse.wireshark
List Files with Human Readable Format with option lh With combination of -lh option, shows sizes in human readable format. # ls -lh total 176K -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap -rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents drwxr-xr-x. 4 root root 4.0K Aug 16 02:55 Downloads -rw-r--r--. 1 root root 21K Aug 12 12:42 fbcmd_update.php -rw-r--r--. 1 root root 46K Jul 31 09:58 index.html -rw-r--r--. 1 root root 48K Jul 31 02:17 install.log -rw-r--r--. 1 root root 12K Jul 31 02:13 install.log.syslog drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos List Files and Directories with / Character at the end Using -F option with ls command, will add the / Character at the end each directory. # ls -F 0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/ anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/ List Files in Reverse Order The following command with ls -r option display files and directories in reverse order. # ls -r Videos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfg Templates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap
Recursively list Sub-Directories ls -R option will list very long listing directory trees. See an example of output of the command. # ls -R total 1384 -rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log -rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log./httpd: total 132 -rw-r--r-- 1 root root 0 Aug 19 03:14 access_log -rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812./lighttpd: total 68 -rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log -rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819./nginx: total 12 -rw-r--r--. 1 root root 0 Aug 12 03:17 access.log -rw-r--r--. 1 root root 390 Aug 12 03:17 access.log-20120812.gz Reverse Output Order With combination of -ltr will shows latest modification file or directory date as last. # ls -ltr total 176 -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
Sort Files by File Size With combination of -ls displays file size in order, will display big in size first. # ls -ls total 176 -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap Display Inode number of File or Directory We can see some number printed before file / directory name. With -i options list file / directory with inode number. # ls -i 20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates 23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos 23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public Shows version of ls command Check version of ls command. # ls --version ls (GNU coreutils) 8.4 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Show Help Page List help page of ls command with their option. # ls --help Usage: ls [OPTION]... [FILE]... List Directory Information With ls -l command list files under directory /tmp. Wherein with -ld parameters displays information of /tmpdirectory. # ls -l /tmp total 408 drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768 -r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz drwx------. 2 root root 4096 Aug 4 11:20 keyring-6mfjnk drwx------. 2 root root 4096 Aug 16 01:33 keyring-piozjr drwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdm drwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4zdxqvrx drwx------. 2 narad narad 4096 Aug 4 08:16 pulse-udh76exwuvou drwx------. 2 gdm gdm 4096 Aug 21 11:26 pulse-wjtcweuctvhn -rw-------. 1 root root 300 Aug 16 03:34 yum_save_tx-2012-08-16-03-34ljtaa1.yumtx # ls -ld /tmp/ drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/ Display UID and GID of Files To display UID and GID of files and directories. use option -n with ls command. # ls -n total 36 drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloads drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Music drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Pictures -rw-rw-r--. 1 500 500 12 Aug 21 13:06 tmp.txt drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Videos ls command and it s Aliases
We have made alias for ls command, when we execute ls command it ll take -l option by default and display long listing as mentioned earlier. # alias ls="ls -l" Note: We can see number of alias available in your system with below alias command and same can be unalias as shown below example. # alias alias cp='cp -i' alias l.='ls -d.* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' To remove an alias previously defined, just use the unalias command. # unalias ls Below you can see the more example: Ls /usr
ls-la Ls ld
4. Mkdir The Unix/Linux mkdir command is used to create new Unix/Linux directories (sub-directories). A directory, referred to as a folder in some operating systems, appears to the user as a container for other directories and files. Directories created by mkdir automatically include two hidden directories, one representing the directory just created (and represented by a single dot) and the other representing its parent directory (and represented by two consecutive dots). This can be seen by using the ls (i.e., list) command with its -a option, which tells ls to show all directories and files, (including hidden ones). For example we want to create directory called abc, type mkdir abc and press enter. To see whether folder has created or not type ls as shown below: 5. rmdir rmdir command is used to remove directory. The rmdir command deletes only the empty directories. If a directory contains files or sub directories, then the rmdir command fails. The syntax of rmdir command is rmdir [options] directories The rmdir command options are -p : Removes directory and its parent directories
-v : Provides the diagnostic information of the directory processed Below you see the example where we created one folder called abc which you can list using ls command, we can remove that folder using rmdir command. 6. Touch The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options. Touch Command Options 1. -a, change the access time only 2. -c, if the file does not exist, do not create it 3. -d, update the access and modification times 4. -m, change the modification time only 5. -r, use the access and modification times of file 6. -t, creates a file using a specified time 1. How to Create an Empty File The following touch command creates an empty (zero byte) new file called sheena. # touch sheena
2. How to Create Multiple Files By using touch command, you can also create more than one single file. For example the following command will create 3 files named, sheena, meena and leena. # touch sheena meena leena 3. How to Change File Access and Modification Time To change or update the last access and modification times of a file called leena, use the - a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name. # touch -a leena The most popular Linux commands such as find command and ls command uses timestamps for listing and finding files. 4. How to Avoid Creating New File Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists. # touch -c leena 5. How to Change File Modification Time If you like to change the only modification time of a file called leena, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file. # touch -m leena
6. Explicitly Set the Access and Modification times You can explicitly set the time using -c and -t option with touch command. The format would be as follows. # touch -c -t YYDDHHMM leena For example the following command sets the access and modification date and time to a file leena as 17:30(17:30 p.m.) December 10 of the current year (2012). # touch -c -t 12101730 leena Next verify the access and modification time of file leena, with ls -l command. # ls -l total 2 -rw-r--r--. 1 root root 0 Dec 10 17:30 leena 7. How to Use the time stamp of another File The following touch command with -r option, will update the time-stamp of file meena with the time-stamp of leena file. So, both the file holds the same time stamp. # touch -r leena meena 8. Create a File using a specified time If you would like to create a file with specified time other than the current time, then the format should be.
# touch -t YYMMDDHHMM.SS tecmint For example the below command touch command with -t option will gives the tecmint file a time stamp of 18:30:55 p.m. on December 10, 2012. # touch -t 201212101830.55 tecmint You can see also list your empty file after creation using ls command. 7. Rm Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it. Remove all files in the working directory. rm will not prompt you for any reason before deleting them. For example, if we want to delete any file just type, Syntax: rm file_name
8. Cp cp command is a command to create copy of files and directories. For example, below you can find that we have created one folder called hello using mkdir command once you create use ls command to see whether your folder is created or not. Once it is created try to copy file with.txt extension which means whatever the file name will be if it is with.txt extension that all files will be copied to the specified folder hello.
Syntax: cp *.* folder_name Here first star means file_name and the second star means extension. Below we said cp *.txt hello Here * means any file name with.txt extension will be copied to hello folder 9. Mv The command to move files is mv. It's very simple and one of the first commands you will learn on the platform. The mv command does one thing it moves a file from one location to another. mv (short for move) is a Unix command that moves one or ore files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories (i.e., the source and the
target) involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated. For example, we want to move one jpg file called download.jpg to another folder called Music then you can write the following command: mv download.jpg Music and press enter. Some more mv command examples Move main.c def.h files to /home/usr/rapid/ directory: $ mv main.c def.h /home/usr/rapid/ Move all C files in current directory to subdirectory bak: $ mv *.c bak Move all files in subdirectory bak to current directory: $ mv bak/*. Rename file main.c to main.bak: $ mv main.c main.bak
Rename directory bak to bak2: $ mv bak bak2 10. Head head, by default, prints the first 10 lines of each FILE to standard output. With more than one FILE, it precedes each set of output with a header identifying the file name. If no FILE is specified, or when FILE is specified as a dash ("-"), head reads from standard input. head myfile.txt Display the first ten lines of myfile.txt. head -15 myfile.txt Display the first fifteen lines of myfile.txt. head myfile.txt myfile2.txt Display the first ten lines of both myfile.txt and myfile2.txt, with a header before each that indicates the filename. head -n 5 myfile.txt myfile2.txt Displays only the first 5 lines of both files. head -c 20 myfile.txt Will output only the first twenty bytes (characters) of myfile.txt. Newlines count as a single character, so if head prints out a newline, it will count it as a byte. head -n 5K myfile.txt Displays the first 5,000 lines of myfile.txt. head -c 6M myfile.txt Displays the first six megabytes. head myfile.txt myfile2.txt - Display the first ten lines of myfile.txt, myfile2.txt, and standard input.
head -n 4 *.txt Display the first four lines of every file in the working directory whose filename ends in the extension.txt. More examples: Head /etc/passwd Head -5 /etc/passwd
11. Tail As we have seen head command in detail same way you can use tail command. Below are some of the examples. Tail /etc/services It will give you by default last 10 lines. Tail -3 /etc/services
12. Which The Linux which command is used to find the location of a program. which returns the pathnames of the files (or links) which would be executed in the current environment, had the filename (or filenames) been given as a command (or commands) in a strictly POSIXconformant shell. It does this by searching the paths in the PATH environment variable for executable files matching the names of the arguments. For example, type which ls which will show the location of it. 13. Strings For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the options below) and are followed by an unprintable character. By default, it only prints the strings from the initialized and loaded sections of object files; for other types of files, it prints the strings from the whole file. strings is mainly useful for determining the contents of non-text files. For example, you can type Strings /bin/ls and can see the following output.
14. Chmod On Linux and other Unix-like operating systems, there is a set of rules for each file which defines who can access that file, and how they can access it. These rules are called file permissions or file modes. The command name chmod stands for "change mode", and it is used to define the way a file can be accessed. Let's say you are the owner of a file named myfile, and you want to set its permissions so that: 1. the user can read, write, and execute it; 2. members of your group can read and execute it; and 3. others may only read it. chmod u=rwx,g=rx,o=r myfile This is an example of using symbolic permissions notation. The letters u, g, and o stand for "user", "group", and "other". The equals sign ("=") means "set the permissions exactly like this," and the letters "r", "w", and "x" stand for "read", "write", and "execute", respectively. The
commas separate the different classes of permissions, and there are no spaces in between them. Here is the equivalent command using octal permissions notation: chmod 754 myfile Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 4, 2, 1, and 0: 4 stands for "read", 2 stands for "write", 1 stands for "execute", and 0 stands for "no permission." So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1(read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute). Now you can use ls alt to view the file current modes.
Now if we want to make folder called file1 using mkdir command as shown below. Once you create folder change the mode using: chmod 777 file1 Now if you want to see the mode you have changed type ls -alt