Linux shell commands to Interact with the File System


Hello dear readers,
   Now that we’ve got acquainted with Linux Operating System and we’ve seen how to partition our hard drive and what is the basic structure of the File System, it’s time to further discover this amazing OS. In today’s article we will learn about some of the most important shell commands that are used to navigate and interact with the Linux File System. Most commands described in this article may be already known by most of you but, I’ll write and explain them for any novice.
   When navigating in the Linux File System you will need to know the paths used to gain access to resources. You can imagine the File System as a tree with several branches. That being said, there are three ways to describe a path to a file:

absolute – paths that start with root (/) and describe the complete path to a resource. For example /var/www/icons/a.gif. When you interact with a file, its absolute path is loaded in RAM so any command executed will apply it to the file’s absolute path. For example, if I type cat a.gif when I’m in the icons directory, the OS will actually execute cat /var/www/icons/a.gif

relative – they have as reference the current directory. There are two symbols used to describe a relative path:

Linux tail command

  • .  – it’s used to describe the current directory. For example, if I navigate to /usr/bin, I can execute tail command by typing ./tail. This actually tells the OS to execute the command from the current directory.
  • Linux File System .. – refers to the parent directory. Let’s take our last example: the absolute path to our current location in the File System is /usr/bin/tail. The absolute path to one of my user home directory is /home/dan. If I want to change the path to the user’s personal directory from my current location, I would type cd ../../home/dan. So basically this command means go back two parent folders (/bin and /usr) and then navigate to /home/dan. Simply put, “..” refers to the parent directory:
Linux tilde character~ (tilde character) – from any directory you can change location or make reference to a file from your personal /home directory using this character. For example, the absolute path to my /Desktop directory would be /home/dan/Desktop but, I can simply use ~/Desktop to refer to this location.
Navigation commands used within Linux:
cd (change directory) – used to jump from one location to another. This command is also available in Windows Systems (Batch and Powershell) and has the same functionality:
Linux Change Directory command
pwd – displays the absolute path of the current working directory:
pwd command
Listing files and directories:
Linux ls commandls – used to display files and directories from the current directory. You can specify relative and absolute paths when using this command. Note that ls can be used with multiple arguments. You can view the help page by typing ls –help or man ls to view its man page. Without any arguments, ls will display files and directories from the current location:
I usually use ls with -la arguments. Remember that in Linux, command arguments can be written individually or combined ( ls -a -l = ls -al):
-a – display all files (“hidden files”). Config files usually start with .(dot) and are not shown by default.
-l – long listing. Displays all info relevant to a specified resource. Let’s take the following example and explain what each section describes. I’ve changed the working directory to /etc and typed ls -la
ls with long listing command in Linux
As you see from the command’s output, there are several sections for each file. Let’s take bashrc file and explain each section:
Linux File System tutorial
  1. informs the user that this is a file, for directories you can see there is a “d” in front of the line
  2. there are 9 characters that describe system permissions (rw-r–r–). we will discuss about permissions in a future article
  3. 1 is the number of hard links that point to this file. We will learn about hard links in a future article
  4. the first root is the owner of the file
  5. second root is the group that has ownership on the file
  6. 2681 – file size in bits. We can add another argument to our command to make the file size easier to read. Type ls -alh where -h stands for human readable and you will see that the sizes are displayed in kilo. Note that for directories, the command will display the directory file size and not the size of all files contained in the directory
  7. 2013 last modification date
  8. file name
ls recursive command
   Another parameter that you can use with ls command is -R (recursive). This argument displays each file and directory and their containing files and directories and so on.
   Other cool parameters that can be used with ls command are -d, -i but, I’ll let you folks discover their usage. Explore the help menu and the man page and you will be able to master the ls command 🙂
Interacting with disk and file size
du (disk usage) – used to view file size in a particular location. Can be used with the -h parameter to view sizes in kilo. You can either use it on a file or on a folder; when executing the command on a folder, it will display the size of all files and directories contained:
Disk usage command Linux
Linux du command
   -c adds the total size of all files at the end of the output ; -s displays the size of the argument passed to the command so, for a directory, will display its total size. I’ve combined these two parameters to see that they have the same output.
Linux Disk free commanddf (disk free) – Displays all disk information from all mounted devices (hard drives, external shares, etc.). Can be used with the -h parameter to have a better output of the command:
Create and remove files and directories
mkdir – creates directories in the specified path. Can be used to create multiple folders at the same time. -p parameter can be used to create a whole path to a specified resource:
mkdir command
touch – used to create files within the Linux File System. You can specify multiple files to be created from one execution of the command. For example:
touch command
Linux rm command
rm – removes file and directories. Can be used with the following parameters: -i – prompts with every removal; -r – recursively delete, used to delete directories; -f – forces the removal of a directory
Copy and move files and directories
Linux copy commandcp  – copy files and directories from one location to another. You can specify one ore more arguments and you can copy multiple files and directories at the same time. For example: cp f4 f10 ; cp f1 f2 file1/  . To copy entire directories you’ll need to add the -r parameter (recursive):
mv – moves or renames files and directories. It has similar functionality as the cp command which means that you can add multiple arguments and copy files and directories at the same time:
Examples:
mv f3 f4 folder1/
mv f5 newfilename
mv f1 f10 f2 f6 file2 newfilename folder1/
Linux move command
Interact with files and directories content
cat – used to display files content
Linux cat command
less – used to control the listing when there is a long output. For example:
cd /
ls -R | less
using space key you control the display of pages
Linux less command
tail and head – used to extract a number of lines within a file. head is used to extract lines from the beginning of the file while tail extracts them from the end
important arguments that can be used with tail :
   -n Number – displays the last Number lines
   -n +Number – displays the last lines starting with the +Number position
   -f – used to tail the output of a log file in real time
important arguments that can be used with head:
   -n Number – displays the first Number lines
wc – “word count” it’s used to count the number of lines, characters or words within a file or from a command output. Arguments that can be used with the wc command include -w ( displays number of words), -l (displays number of lines), -m (displays number of characters)
file – displays information about a file. For example:
Linux file command
File search 
find – used to search directly in the File System. It’s search command is accurate but, can take a long period of time if the OS is filled with data. Can be used with different arguments, some of the most important are:
   –typef (file), d(directory), l(simlink)
   –name – searches for a file who’s name contains the specified pattern
   –user – searches for the owner of the file
   –size – maximum or minimum size of a file
The following image displays an example of using find command. Note that I’ve used -type f option to search only for files within the root:
find / -name “login” -type f
Linux find command
You can use regex for searching for particular patterns
find / -name \*login\* -type f
 
Other types that can be searched with find command are:
 -type c
              File is of type c:
 
              b      block (buffered) special
 
              c      character (unbuffered) special
 
              d      directory
 
              p      named pipe (FIFO)
 
              f      regular file
 
              l      symbolic  link;  this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken.  If you want to search for symbolic links when -L is in effect, use
                     -xtype.
 
              s      socket
 
              D      door (Solaris)
 
slocate – searches within a local encrypted database which indexes all data from your hard drive. By default, the db update is made automatically but, you can change that behavior from the config file or manually update it. If the database has indexed all info, the search speed is highly increased rather than searching using find command
database file: /var/lib/slocate/slocate.db 
configuration file: /etc/updatedb.conf
grep – “The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. grep is considered as one of the most useful commands on Unix and other Linux operating systems.” Link here
 
egrep – “Search for a pattern using extended regular expressions. egrep is essentially the same as running grep with the -E option.” Link here
 
  That’s about it for this article folks, hope it will serve you well in enhancing your Linux knowledge. Note that most commands written in this post support many arguments that were not described in this article so please, don’t forget to use man pages or help menus to discover all features available with each command. In the following articles we will continue our Linux journey and we will learn more features supported by this Operating System. Don’t forget to enjoy your day and stay tuned for the following articles!
Advertisements

2 thoughts on “Linux shell commands to Interact with the File System

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s