Table of Content
Table of Content
File/Directory Management
Directory Management
Home Directory
Absolute/Relative Pathnames
Listing Directories
Creating Directories
Creating Parent Directories
Removing Directories
Changing Directories
Renaming Directories
File Management
Create a blank File with TOUCH command
Create a blank File with CAT command
Create or Editing Files with Nano
Moving and Renaming Files
Copying Files and Directories
Removing Files and Directory
File and Directory Permissions Management
Understanding File permissions and Ownership
File Permission Scheme
Permission Values and its meaning
Changing permission and Ownership
Find Files and Directories
Basic options
Search only files or only directories
Find files with certain permissions
Search Files Based on Owners and Groups
Search file and directories based on modification date and time
Some advanced operations
Find Content from File or Directory
Find content from file
Find Content from Directory
Compression and Decompression Commands
ZIP Command
Installing Zip utility
Zipping individual files
Listing of Zip content
Modify Zip archive without extract
Extract Zip Archive
gZip Commands
Create/Compress gZip archive
Extract/Decompress gZip archive
Display gZip file information
bZip2 Command
Create/Compress bZip2 archive
Extract/Decompress bZip2 archive
Display bZip2 file information
TAR command
Creating an archive using TAR command
Extracting (untar) an archive using TAR command
Listing an archive using TAR command
MySQL Commands
Connect with MySQL Server
Importing Database
Exporting/Dumping Database
Useful Commands
Downloading file from URL using WGET
Single file download
Download file with different name
Download multiple file with http and ftp protocol
Read URL’s from a file and download all
SSH: Secure Remote Login and Command Execution
SCP: Secure Copy
Configuring Cron Job
View Cron Job Entries
How to Edit Cron Job Entries?
Linux Cron Job Format
Scheduling a Job For a Specific Time
Commands for Reading File Content
CAT Command
HEAD Command
TAIL Command
LESS command
File/Directory Management
Directory Management
Linux uses a hierarchical structure for organizing files and directories. This structure is often referred to as a
directory tree . The tree has a single root node, the slash character ( /), and all other directories are
contained below it.
Home Directory
Change directory to logged-in user's home directory
$ cd ~
$ cd ~username
$ cd -
Absolute/Relative Pathnames
Absolute path example
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
./chem/notes
../../personal/res
To determine where you are within the filesystem hierarchy at any time, enter the command pwd to print the
current working directory −
$ pwd
Listing Directories
To list the files in a directory you can use the following syntax
$ ls dirname
$ ls -a
$ ls -l
or
$ ll
Creating Directories
Directories are created by the following command
$ mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to create. For example, the
command −
$ mkdir mydir
Creates the directory mydir in the current directory. Here is another example
$ mkdir /tmp/test-dir
This command creates the directory test-dir in the /tmp directory. The mkdir command produces no output
if it successfully creates the requested directory.
If you give more than one directory on the command line, mkdir creates each of the directories. For
example
Creates the directories docs and pub under the current directory.
$ mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
In such cases, you can specify the -p option to the mkdir command. It creates all the necessary directories
for you. For example −
$ mkdir -p /tmp/amrood/test
Removing Directories
Directories can be deleted using the rmdir command as follows
$ rmdir dirname
Note − To remove a directory make sure it is empty which means there should not be any file or subdirectory
inside this directory.
Changing Directories
You can use the cd command to do more than change to a home directory: You can use it to change to any
directory by specifying a valid absolute or relative path. The syntax is as follows −
$ cd dirname
Here, dirname is the name of the directory that you want to change to. For example, the command
$ cd /usr/local/bin
Changes to the directory /usr/local/bin. From this directory you can cd to the directory /usr/home/amrood
using the following relative path
$ cd ../../home/amrood
Renaming Directories
The mv (move) command can also be used to rename a directory. The syntax is as follows
$ mv olddir newdir
File Management
Create a blank File with TOUCH command
Many commands and programs can create files. The most basic method of creating a file is with the touch
command. This will create an empty file using the name and location specified.
First, we should make sure we are in our home directory, since this is a location where we have permission
to save files. Then, we can create a file by typing
$ touch myfile.txt
Hello world.
^Z
The nano command is one of the simplest command line Linux text editors, and is a great starting point for
beginners. In that it occupies the entire terminal for the duration of its use.
The nano editor can open existing files, or create a file. If you decide to create a new file, you can give it a
name when you call the nano editor, or later on, when you wish to save your content.
$ nano readme.txt
The nano application will open the file (which is currently blank). The interface looks something like this:
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
Along the top, we have the name of the application and the name of the file we are editing. In the middle,
the content of the file, currently blank, is displayed. Along the bottom, we have a number of key
combinations that indicate some basic controls for the editor. For each of these, the “^” character means
the “CTRL” key.
CTRL-G
When you are finished browsing the help, type CTRL+X to get back to your document. Type in or modify
any text you would like. For this example, we'll just type these 2 sentences
Hello World.
To save our work, we can type CTRL+O (not zero). It will ask you to confirm the name of the file you wish to
save to:
^G Get Help M-D DOS Format M-A Append M-B Backup File
As you can see, the options at the bottom have also changed. These are contextual, meaning they will
change depending on what you are trying to do. If readme.txt is still the file you wish to write to, hit
"ENTER".
If we make some additional changes and wish to save the file and exit the program, we will see a similar
prompt. Add a new line, and then try to exit the program by typing CTRL+X.
If you have not saved after making your modification, you will be asked whether you wish to save the
modifications you made:
Y Yes
N No ^C Cancel
You can type Y to save your changes, N to discard your changes and exit, or CTRL+C to cancel the exit
operation. If you choose to save, you will be given the same file prompt that you received before,
confirming that you want to save the changes to the same file. Press ENTER to save the file and exit the
editor.
$ mv my_file my_dir
$ mv my_dir/my_file .
$ mv my_dir my_files
$ cp file3 file4
$ cp -r my_dir other_dir
$ cp file1 other_dir/
$ rm my_file
Removing file forcibly without any confirmation
$ rm -f my_file
$ rm -r again
Once again, it is worth reiterating that these are permanent actions. Be entirely sure that the command you
typed is the one that you wish to execute.
$ ls -l some_file
and so on...
Value Meaning
(rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights.
700 This setting is useful for programs that only the owner may use and must be kept private from
others.
666 (rw-rw-rw-) All users may read and write the file.
(rw-r—r--) The owner may read and write a file, while all others may only read the file. A
644
common setting for data files that everybody may read, but only the owner may change.
(rw-------) The owner may read and write a file. All others have no rights. A common setting for
600
data files that the owner wants to keep private.
For Directories
Value Meaning
(rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the
777
directory and delete files in the directory. Generally not a good setting.
(rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot
755 create files nor delete them. This setting is common for directories that you wish to share with
other users.
(rwx------) The directory owner has full access. Nobody else has any rights. This setting is
700
useful for directories that only the owner may use and must be kept private from others.
You can change owner of file using chown. Owner of file should be one particular user or group of users.
$ find
$ find .
$ find . -print
$ find ./test
Note that all sub directories are searched recursively. So this is a very powerful way to find all files of a
given extension.
Ignore the case
Its always better to wrap the search term (name parameter) in double or single quotes. Not doing so will
seem to work sometimes and give strange results at other times.
The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in
the current directory.
It is possible to use multiple criterias when specifying name and inverting. For example
The above find command looks for files that begin with abc in their names and do not have a php
extension. This is an example of how powerful search expressions can be build with the find command.
The above command search for files ending in either the php extension or the txt extension.
Only files
Only directories
$ find ./test -type d -name "abc*"
Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden
files.
The find command can be used to find files with a specific permission using the "perm" option. The
following command searches for files with the permission 0664
This can be useful to find files with wrong permissions which can lead to security issues. Inversion can also
be applied to permission checking.
The "perm" option of find command accepts the same mode string like chmod. The following command
finds all files with permission 644 and sgid bit set.
Similarly use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal
numbers.
Note that the "2>/dev/null" removes those entries that have an error of "Permission Denied"
We could also specify the name of the file or any name related criteria along with user criteria.
$ find / -mtime 50
$ find / -atime 50
Find all files that were modified between 50 to 100 days ago.
The following command will display the 5 largest file in the current directory and its subdirectory. This may
take a while to execute depending on the total number of files the command has to process.
Similarly when sorted in ascending order, it would show the smallest files first
The same operating can be carried out with directories, just put type d, instead of type f.
Lets take another example where we want to delete files larger than 100MB
To perform a case-insensitive search with the grep command, just add the -i option, like this:
You can use a different version of the grep command to search for multiple patterns at one time. To do this,
just use the egrep command instead of grep, like this:
Using this more-advanced search, a user named "alfred" would not be matched, because the letter "a" will
be in the first column:
If you are looking through a lot of files for a pattern, and you just want to find the names of the files that
contain your pattern (or "patterns", as shown with egrep) but do not want to see each individual grep
pattern match just add the -l (lowercase letter L) to your grep command, like this:
Of course you can also combine grep command arguments, so if you did not happen to know how to
capitalize "StartInterval" in that previous example, you could just add the -i argument to ignore case, like
this:
To show the line numbers of the files that match your grep command, just add the -n option, like this:
In above command
r or R is recursive,
Along with these, --exclude or –include parameter could be used for efficient searching. Something like
below:
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
Above will exclude searching all the files ending with .o extension. Just like exclude file it is possible to
exclude/include directories through –exclude-dir and –include-dir parameter, the following shows how to
integrate --exclude-dir:
To compress fast use -1 option and for compress better ratios use -9
Zipping a folder is a tricky thing as by default zip will not zip entire folder content such as sub folders and
files let us see how a zip command work by default on a folder
$ zip abc.zip abc/
Actually there are subdirectories and files in abc folder, in order to zip all content of a folder use -r option
How to zip files which are not located in present directory? Suppose I want to zip /home/surendra/test/abc
folder in /tmp and I am at /var folder?
$ cd /var
$ zip -r /tmp/abc.zip /home/surendra/test/abc
$ unzip -l abc.zip
or
$ less abc.zip
or
$ zipinfo -1 abc.zip
To update a particular file which is modified and we want to update our zip file with this update use -u
option
$ unzip abc.zip
gZip Commands
Create/Compress gZip archive
To create a *.gz compressed file:
$ gzip test.txt
$ gzip -d test.txt.gz
$ gzip -l *.gz
bZip2 Command
Create/Compress bZip2 archive
To create a *.gz compressed file:
$ bzip2 test.txt
$ bzip2 -d test.txt.bz2
TAR command
Creating an archive using TAR command
Creating an uncompressed tar archive using option cvf This is the basic command to create a tar archive.
Creating a tar gzipped archive using option cvzf, The above tar cvf option, does not provide any
compression. To use a gzip compression on the tar archive, use the z option as shown below.
gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less
than gzip.
In above command
Extract a gzipped tar archive ( *.tar.gz ) using option xvzf, use the option z for uncompressing a gzip tar
archive.
$ tar -xvfz archive_name.tar.gz
Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf, use the option j for uncompressing a bzip2 tar
archive.
Note: In all the above commands v is optional, which lists the file being processed.
View the *.tar.gz file content without extracting using option tvzf. You can view the *.tar.gz file content
before extracting as shown below.
View the *.tar.bz2 file content without extracting using option tvjf. You can view the *.tar.bz2 file content
before extracting as shown below.
MySQL Commands
Connect with MySQL Server
To specify the hostname and username explicitly, as well as a password, supply appropriate options on the
command line:
or
or
p – password of mysql user, the password value is optional, it prompt for password. But if you
specify password in command line then no keep space between p and password.
Importing Database
We can import whole database from sql dump file by using sql command interface as mentioned below.
This is very faster then import database from PhpMyAdmin and for import large size of dump.
or
You can see on above commands line pipeline (|) is linking 2 independent command in one stream. First
command extract compressed file and pipeline is pass output as input for second command. In unzip
command -p option is used for pipes the output.
Exporting/Dumping Database
We can export whole database from sql dump file by using sql command interface as mentioned below.
This is very faster then export database from PhpMyAdmin and for import large size of dump.
Useful Commands
Downloading file from URL using WGET
Single file download
The command will download single file and stores in a current directory. It also shows download progress,
size, date and time while downloading.
$ wget http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
Download file with different name
Using -O (uppercase) option, downloads file with different file name. Here we have given wget.zip file name
as show below.
$ wget -i /wget/tmp.txt
Login to a remote host on which you have an account under the same username as the host you are
currently logged into:
$ ssh collie.stanford.edu
Login to a remote host on which you have an account under a different username than the host you are
currently logged into:
$ ssh [email protected]
Run a command on a remote host on which you have an account under the same user name:
Run a command on a remote host on which you have an account under a different user name:
Use ssh with tar to move a directory of files between 2 machines as an alternative to scp:
$ tar -cvf - images/dogs | ssh [email protected] '(cd new_images; tar -xf -)'
Copy a local file to the login directory of a remote host on which you have an account under the same user
name
Copy a remote file in a subdirectory to the local host (and current directory) on which you have an account
under the same user name:
$ scp collie.stanford.edu:images/dogs.gif .
Copy a local file to a remote host on which you have an account under a different user name
Copy an entire local subdirectory to a remote host on which you have an account under the same user
name
$ crontab -l
*/10 * * * * /home/ramesh/check-disk-space
Password:
$ crontab -l
To view crontab entries of other Linux users as sudo user mentioned below
$ crontab -u username -l
00 09-18 * * * /home/myscript/check-db-status
To edit a crontab entries, use crontab -e as shown below. By default this will edit the current logged-in
user's crontab.
$ crontab -e
Password:
$ crontab -e
To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.
$ crontab -u username -e
Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.
30 08 10 06 * /home/ramesh/full_backup
30 – 30th Minute
08 – 08 AM
10 – 10th Day
00 11,16 * * * /home/ramesh/bin/incremental_backup
11,16 – 11 AM and 4 PM
* – Every day
* – Every month
00 */2 * * * /home/ramesh/bin/incremental_backup
00 – 0th Minute (Top of the hour)
* – Every day
* – Every month
This example checks the status of the database everyday (including weekends) during the working hours 10
a.m – 7 p.m
00 09-18 * * * /home/ramesh/bin/check-db-status
10-19 – 10 am to 7pm
* – Every day
* – Every month
This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the
working hours 9 a.m – 6 p.m.
09-19 – 10 am to 7pm
* – Every day
* – Every month
This will execute the system annual maintenance using annual_maintenance shell script at 00:00 on Jan 1st
for every year.
@yearly /home/ramesh/red-hat/bin/annual_maintenance
This will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly /home/ramesh/suse/bin/tape-backup
Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell script at 00:00
on every day.
Using the @reboot cron keyword, this will execute the specified command once after the machine got
booted every time.
@reboot CMD
$ cat my_file.txt
Append the content to a file, use >> redirection symbol as shown below.
HEAD Command
Display first 10 lines by default
$ head example.txt
Display first N lines use the -n option to print the first n lines from the file.
$ head -n 5 example.txt
$ head -c 8 example.txt
$ head -n -5 flavours.txt
TAIL Command
Display last 10 lines, By default, the tail command prints the last 10 lines from the file.
$ tail example.txt
Display last N lines use the -n option to print the last n lines from the file.
LESS command
“less” command is used to view files instead of opening the file. This post describes “less” command used
in Linux along with usage.
$ less my_file.txt
...
{Content of file}
...
my_file.txt (END)
In order to quit from the prompt press “q” from the keyboard.
To Clear screen before displaying , It clears the screen and shown only the contents of the file.
$ less -c sample.txt
...
{Content of file}
...
...
my_file.txt (END)
$ less +4 my_file.txt
...
...
my_file.txt (END)
● b: Previous page.
● N: Go to previous match.
● ‘^ or g: Go to start of file.
● ‘$ or G: Go to end of file.
● s: Save current content (got from another program like grep) in a file.
● =: File information.
● F: continually read information from file and follow its end. Useful for logs watching. Use CTRL+C to
exit this mode.
● h: Help.
● q or zz: Quit.
Forward Search Navigation
● / – search for a pattern which will take you to the next occurrence.
● ? – search for a pattern which will take you to the previous occurrence.
● 5j – 5 lines forward.
● CTRL+G – show the current file name along with line, byte and percentage statistics.