0% found this document useful (0 votes)
66 views32 pages

Linux File and Directory Management Guide

The document provides information on various Linux file and directory management commands. It discusses directory navigation and manipulation commands like cd, mkdir, rmdir. It covers file manipulation commands like touch, cat, nano and mv to create, edit, move and rename files. It also summarizes commands for permissions (chmod), searching files and directories (find, grep), compression (tar, gzip, bzip2), and MySQL database management.

Uploaded by

ahvaghasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views32 pages

Linux File and Directory Management Guide

The document provides information on various Linux file and directory management commands. It discusses directory navigation and manipulation commands like cd, mkdir, rmdir. It covers file manipulation commands like touch, cat, nano and mv to create, edit, move and rename files. It also summarizes commands for permissions (chmod), searching files and directories (find, grep), compression (tar, gzip, bzip2), and MySQL database management.

Uploaded by

ahvaghasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 ~

Change directory to other user's home directory

$ cd ~username

Change directory to previous directory

$ cd -

Absolute/Relative Pathnames
Absolute path example

/etc/passwd

/users/sjones/chem/notes

/dev/rdsk/Os3

Relative path examples

./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

Following is the example to list including hidden directory and files

$ ls -a

Following is the example to list with detailed and long output

$ 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

$ mkdir docs pub

Creates the directories docs and pub under the current directory.

Creating Parent Directories


Sometimes when you want to create a directory, its parent directory or directories might not exist. In this
case, mkdir issues an error message as follows

$ mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";

No such file or directory

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

Above command creates all the required parent directories.

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.

You can remove multiple directories at a time as follows

$ rmdir dirname1 dirname2 dirname3

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

You can rename a directory mydir to yourdir as follows


$ mv mydir yourdir

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 [Link]

Create multiple file with different location

$ touch /var/www/[Link] /home/demo/[Link]

Create a blank File with CAT command


Using cat command you can create file and input text. After complet input, just type CTRL+Z.

$ cat > [Link]

Hello world.

This is my first file.

^Z

[1]+ Stopped cat > [Link]

Create or Editing Files with Nano


Currently, we know how to manipulate files as objects, but we have not learned how to actually edit them
and add content to them.

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.

We can open the [Link] file for editing by typing:

$ nano [Link]

The nano application will open the file (which is currently blank). The interface looks something like this:

GNU nano 2.2.6 File: file1


[ Read 0 lines ]

^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos

^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

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.

To get help from within the editor, type:

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.

Here is some text.

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:

File Name to Write: file1

^G Get Help M-D DOS Format M-A Append M-B Backup File

^C Cancel M-M Mac Format M-P Prepend

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 [Link] 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:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

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.

Moving and Renaming Files


We can move a file to a new location using the mv command. For instance, we can move my_file into the
my_dir directory by typing

$ mv my_file my_dir

Move back file to current working directory

$ mv my_dir/my_file .

So to rename the my_dir directory to my_files, we could type:

$ mv my_dir my_files

Copying Files and Directories


Copy one file to other file in same directory

$ cp file3 file4

Copy the my_dir directory structure to a new structure called other_dir

$ cp -r my_dir other_dir

Copy file to another directory

$ cp file1 other_dir/

Removing Files and Directory


Removing file, this will prompt for confirm action

$ rm my_file
Removing file forcibly without any confirmation

$ rm -f my_file

Removing directory recursively (directory contains files or subdirectories)

$ 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.

File and Directory Permissions Management


Understanding File permissions and Ownership
In Linux (like Unix), each file and directory on your system is assigned access rights for the owner of the file,
the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write
a file, and to execute a file (i.e., run the file as a program).

$ ls -l some_file

-rw-rw-r-- 1 me me 1097374 Sep 26 18:48 some_file

File Permission Scheme

rwx rwx rwx = 111 111 111 = 777

rw- rw- rw- = 110 110 110 = 666

rwx --- --- = 111 000 000 = 700

and so on...

rwx = 111 in binary = 7

rw- = 110 in binary = 6

r-x = 101 in binary = 5

r-- = 100 in binary = 4

Permission Values and its meaning


For Files

Value Meaning

(rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a


777
desirable setting.
(rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and
755
execute the file. This setting is common for programs that are used by all users.

(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.

Changing permission and Ownership


For change permission of file

$ chmod 600 my_file

For change permission of directory, itself only not recursively

$ chmod 777 my_dir

For change permission of directory recursively for its content


$ chmod -R 777 my_dir

Changing ownership of file.

$ chown <username_or_group_name> some_file

You can change owner of file using chown. Owner of file should be one particular user or group of users.

Changing ownership of directory recursively

$ chown -R <username_or_group_name> my_directory

Changing Group Ownership from one group to another

$ chgrp <other_group_name> my_file

Find Files and Directories


Basic options
List all files in current and sub directories

$ find

The command is same as the following

$ find .

$ find . -print

Search specific directory or path

$ find ./test

The following command searches for files by their name.

$ find ./test -name "[Link]"

We can also use wildcards

$ find ./test -name "*.php"

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

$ find ./test -iname "*.php"

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.

Limit depth of directory traversal

$ find ./test -maxdepth 2 -name "*.php"

$ find ./test -maxdepth 1 -name *.php

The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in
the current directory.

File name match

$ find ./test -not -name "*.php"

File name invert match

$ find ./test ! -name "*.php"

It is possible to use multiple criterias when specifying name and inverting. For example

$ find ./test -name 'abc*' ! -name '*.php'

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.

OR operator for find any matched file

$ find -name '*.php' -o -name '*.txt'

The above command search for files ending in either the php extension or the txt extension.

Search only files or only directories

$ find ./test -name abc*

Only files

$ find ./test -type f -name "abc*"

Only directories
$ find ./test -type d -name "abc*"

Search multiple directories together

$ find ./test ./dir2 -type f -name "abc*"

Check, that it listed files from 2 separate directories.

Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden
files.

$ find ~ -type f -name ".*"

Find files with certain permissions

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

$ find . -type f -perm 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.

$ find . -type f ! -perm 0777

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.

$ find / -perm 2644

Similarly use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal
numbers.

$ find / -maxdepth 2 -perm /u=s 2>/dev/null

Note that the "2>/dev/null" removes those entries that have an error of "Permission Denied"

Find all Read Only files.

$ find /etc -maxdepth 1 -perm /u=r

The following command will find executable files

$ find /bin -maxdepth 2 -perm /a=x


Search Files Based on Owners and Groups
To find all or single file called [Link] under /root directory of owner root.

$ find . -user bob

We could also specify the name of the file or any name related criteria along with user criteria.

$ find . -user bob -name '*.php'

Find all files that belong to a particular group.

$ find /var/www -group developer

Search file from home directory

$ find ~ -name "[Link]"

Search file and directories based on modification date and time


To find all the files which are modified 50 days back.

$ find / -mtime 50

Find all files that were accessed in the last 50 days.

$ find / -atime 50

Find all files that were modified between 50 to 100 days ago.

$ find / -mtime +50 –mtime -100

Find files modified within the last 1 hour.

$ find /home/bob -cmin -60

To find all the files which are modified in last 1 hour.

$ find / -mmin -60

To find all the files which are accessed in last 1 hour.

$ find / -amin -60

To find all 50MB files, use.


$ find / -size 50M

Find files in a size range

$ find / -size +50M -size -100M

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.

$ find . -type f -exec ls -s {} \; | sort -n -r | head -5

Similarly when sorted in ascending order, it would show the smallest files first

$ find . -type f -exec ls -s {} \; | sort -n | head -5

Find empty files and directories

$ find /tmp -type f -empty

To file all empty directories use the type "d".

$ find ~/ -type d -empty

Some advanced operations


List out the found files, let's say we found files using find command, and now want to list them out as the ls
command would have done. This is very easy.

$ find . -exec ls -ld {} \;

Delete all matching files or directories

$ find /tmp -type f -name "*.txt" -exec rm -f {} \;

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

$ find /home/bob/dir -type f -name *.log -size +10M -exec rm -f {} \;

Find Content from File or Directory


Find content from file
To perform a case-sensitive search with the grep command, just add the -i option, like this:
$ grep "my_string" my_file.txt

To perform a case-insensitive search with the grep command, just add the -i option, like this:

$ grep -i "my_string" my_file.txt

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:

$ egrep 'score|nation|liberty|equal' [Link]

Using this more-advanced search, a user named "alfred" would not be matched, because the letter "a" will
be in the first column:

$ grep '^fred' /etc/passwd

Display only file names with a grep search

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:

$ grep -l 'StartInterval' *.plist

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:

$ grep -il "startinterval" *.plist

To show the line numbers of the files that match your grep command, just add the -n option, like this:

$ grep -n "we" [Link]

Find Content from Directory


Do the following

$ grep -irnwl ./my_dir -e "hello"

In above command

i for ignore case sensitive,

r or R is recursive,

n is line number and


w stands match the whole word.

-l (letter L) can be added to have just the filename.

Along with these, --exclude or –include parameter could be used for efficient searching. Something like
below:

$ grep --include=\*.{c,h} -rnw 'directory' -e "pattern"

This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:

$ grep --exclude=*.o -rnw 'directory' -e "pattern"

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:

$ grep --exclude-dir={dir1,dir2,*.dst} -rnw 'directory' -e "pattern"

Compression and Decompression Commands


ZIP Command
Installing Zip utility
On Ubuntu and Debian based machines

$ apt-get install zip

Syntax for zipping a file or folder.

$ zip [Link] file1 file2 folder1

To compress fast use -1 option and for compress better ratios use -9

$ zip -1 [Link] file1

Zipping individual files


Zip individual files to a zip archive

$ zip [Link] file1 file2 file3

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 [Link] abc/

Actually there are subdirectories and files in abc folder, in order to zip all content of a folder use -r option

$ zip -r [Link] abc/

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/[Link] /home/surendra/test/abc

Listing of Zip content


List all the files stored in a zip file

$ unzip -l [Link]

or

$ less [Link]

or

$ zipinfo -1 [Link]

Modify Zip archive without extract


Delete a file in an archive without extracting entire zip file.

$ zip -d [Link] path/to/file

To update a particular file which is modified and we want to update our zip file with this update use -u
option

$ zip -u [Link] documents/my_doc.doc

Extract Zip Archive


Extract your files from a zip folder

$ unzip [Link]

To extract to a specific directory use -d option


$ unzip [Link] -d /tmp

Extract specific file from an archive

$ unzip [Link] 1/2/5/[Link]

gZip Commands
Create/Compress gZip archive
To create a *.gz compressed file:

$ gzip [Link]

Extract/Decompress gZip archive


To uncompress a *.gz file:

$ gzip -d [Link]

Display gZip file information


Display compression ratio of the compressed file using gzip -l

$ gzip -l *.gz

compressed uncompressed ratio uncompressed_name

23709 97975 75.8% [Link]

bZip2 Command
Create/Compress bZip2 archive
To create a *.gz compressed file:

$ bzip2 [Link]

Extract/Decompress bZip2 archive


To uncompress a *.gz file:

$ bzip2 -d [Link].bz2

Display bZip2 file information


Display compression ratio of the compressed file using bzip2 -l
$ bzip2 -l [Link].bz2

compressed uncompressed ratio uncompressed_name

23709 97975 75.8% [Link]

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.

$ tar -cvfz archive_name.tar dirname/

In the above command:


c – create a new archive
v – verbosely list files which are processed.
f – following is the archive file name

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.

$ tar -cvzf archive_name.[Link] dirname/

In the above command:


z – filter the archive through gzip

Creating a tar bzipped archive using option cvjf

$ tar -cvjf archive_name.tar.gz2 dirname/

In the above command:


j – filter the archive through bzip2

gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less
than gzip.

Extracting (untar) an archive using TAR command


Extract a *.tar file using option xvf, extract a tar file using option x as shown below

$ tar -xvf archive_name.tar

In above command

x – extract files from archive

Extract a gzipped tar archive ( *.[Link] ) using option xvzf, use the option z for uncompressing a gzip tar
archive.
$ tar -xvfz archive_name.[Link]

Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf, use the option j for uncompressing a bzip2 tar
archive.

$ tar -xvfj archive_name.tar.bz2

Note: In all the above commands v is optional, which lists the file being processed.

Listing an archive using TAR command


View the tar archive file content without extracting using option tvf. You can view the *.tar file content before
extracting as shown below.

$ tar -tvf archive_name.tar

View the *.[Link] file content without extracting using option tvzf. You can view the *.[Link] file content
before extracting as shown below.

$ tar -tvfz archive_name.[Link]

View the *.tar.bz2 file content without extracting using option tvjf. You can view the *.tar.bz2 file content
before extracting as shown below.

$ tar -tvfj archive_name.tar.bz2

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:

$ mysql --host=localhost --user=myname --password=mypass mydb

or

$ mysql -h localhost -u myname -pmypass mydb

or

$ mysql -h localhost -u myname -p mydb

In above command line

h – hostname of MySQL Server (its optional if it is localhost)


u – username of mysql user

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.

mydb- is name of database but it is optional

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.

$ mysql -h localhost -u myname -pmypass mydb < [Link]

For import compressed file you can use pipeline

$ tar -xvfz my_db.[Link] | mysql -h localhost -u myname -pmypass mydb

or

$ unzip -p my_db.[Link] | mysql -h localhost -u myname -pmypass mydb

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.

$ mysqldump -h localhost -u myname -pmypass mydb > my_db.sql

For import compressed file you can use pipeline

$ mysqldump -h localhost -u myname -pmypass mydb | gzip > my_db.[Link]

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 [Link]
Download file with different name
Using -O (uppercase) option, downloads file with different file name. Here we have given [Link] file name
as show below.

$ wget -O [Link] [Link]

Download multiple file with http and ftp protocol


Here we see how to download multiple files using HTTP and FTP protocol with wget command at once.

$ wget [Link] [Link]


[Link]

Read URL’s from a file and download all


You can store number of URLs in text file and download them with -i option. Below we have created [Link]
under wget directory where we put series of URL’s to download.

$ wget -i /wget/[Link]

SSH: Secure Remote Login and Command Execution


SSH is used to connect into a remote machine and for executing commands on a remote host.

Syntax: ssh <user_name>@<hostname> [command]

Login to a remote host on which you have an account under the same username as the host you are
currently logged into:

$ ssh [Link]

Login to a remote host on which you have an account under a different username than the host you are
currently logged into:

$ ssh jhawkins@[Link]

Run a command on a remote host on which you have an account under the same user name:

$ ssh [Link] uptime

Run a command on a remote host on which you have an account under a different user name:

$ ssh jhawkins@[Link] uptime

Use ssh with tar to move a directory of files between 2 machines as an alternative to scp:
$ tar -cvf - images/dogs | ssh jhawkins@[Link] '(cd new_images; tar -xf -)'

SCP: Secure Copy


scp copies files between hosts on a network. It uses ssh for data transfer, and uses the same
authentication and provides the same security as ssh

Syntax: scp [-r] user@host1:file1 user@host2:file2

Copy a local file to the login directory of a remote host on which you have an account under the same user
name

$ scp [Link] [Link]:

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 [Link]:images/[Link] .

Copy a local file to a remote host on which you have an account under a different user name

$ scp [Link] jhawkins@[Link]:

Copy an entire local subdirectory to a remote host on which you have an account under the same user
name

$ scp -r images/dogs [Link]:

Configuring Cron Job


View Cron Job Entries
View Current Logged-In User’s Crontab entries, to view your crontab entries type crontab -l from your unix
account as shown below.

$ crontab -l
*/10 * * * * /home/ramesh/check-disk-space

View Root Cron tab entries

Login as root user (su – root) and do crontab -l as shown below.

$ sudo -su root

Password:
$ crontab -l

no crontab for root

View Other Linux User’s Crontabs entries

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

How to Edit Cron Job Entries?


Edit Current Logged-In User’s Crontab entries

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

Edit Root Crontab entries

Login as root user (su – root) and do crontab -e as shown below.

$ sudo -su root

Password:

$ crontab -e

Edit Other Linux User’s Crontab File entries

To edit crontab entries of other Linux users, login to root and use -u {username} -e as shown below.

$ crontab -u username -e

Linux Cron Job Format

MIN HOUR DOM MON DOW CMD

Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

Field Description Allowed Value

MIN Minute field 0 to 59

HOUR Hour field 0 to 23


DOM Day of Month 1-31

MON Month field 1-12

DOW Day Of Week 0-6

CMD Command Any command to be executed.

Scheduling a Job For a Specific Time


The basic usage of cron is to execute a job in a specific time as shown below. This will execute the full
backup shell script (full_backup) on 10th June 08:30 AM.

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

06 – 6th Month (June)

* – Every day of the week

Cron job every day twice in a day


This example executes the specified incremental backup shell script (incremental_backup) at 11:00 and
16:00 on every day. The comma separated value in a field specifies that the command needs to be
executed in all the mentioned time.

00 11,16 * * * /home/ramesh/bin/incremental_backup

00 – 0th Minute (Top of the hour)

11,16 – 11 AM and 4 PM

* – Every day

* – Every month

* –Every day of the week

Cron job every day every 2 hours


This example executes the specified incremental backup shell script (incremental_backup) every 2 hours.

00 */2 * * * /home/ramesh/bin/incremental_backup
00 – 0th Minute (Top of the hour)

*/2 – every 2 hours

* – Every day

* – Every month

* – Every day of the week

Cron Job every day during working hours

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

00 – 0th Minute (Top of the hour)

10-19 – 10 am to 7pm

* – Every day

* – Every month

* – Every day of the week

Cron Job every weekday during working hours

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.

00 09-19 * * 1-5 /home/ramesh/bin/check-db-status

00 – 0th Minute (Top of the hour)

09-19 – 10 am to 7pm

* – Every day

* – Every month

1-5 – Monday to Friday

Schedule a Job For First Minute of Every Year using @yearly

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

Schedule a Cron Job Beginning of Every Month using @monthly

This will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly /home/ramesh/suse/bin/tape-backup

Schedule a Background Job Every Day using @daily

Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell script at 00:00
on every day.

@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"

Execute a Linux Command After Every Reboot using @reboot

Using the @reboot cron keyword, this will execute the specified command once after the machine got
booted every time.

@reboot CMD

Commands for Reading File Content


CAT Command
Display the contents of a file

$ cat my_file.txt

Display contents of more than one file as shown below.

$ cat my_file.txt your_file.txt

Create a file using STDOUT from CAT command

$ cat > my_file.txt


Hello World..!!
^z

Append the content to a file, use >> redirection symbol as shown below.

$ cat >> my_file.txt


How are you?
^z

Copy file content to other file

$ cat my_file.txt >my_file_bkp.txt

Concatenate contents of multiple files into other file


$ cat my_file.txt your_file.txt > our_file.txt

HEAD Command
Display first 10 lines by default

$ head [Link]

Display first N lines use the -n option to print the first n lines from the file.

$ head -n 5 [Link]

Print the first N bytes.

$ head -c 8 [Link]

Print all but not the last N lines

$ head -n -5 [Link]

TAIL Command
Display last 10 lines, By default, the tail command prints the last 10 lines from the file.

$ tail [Link]

Display last N lines use the -n option to print the last n lines from the file.

$ tail -n2 [Link]

Print lines from the Nth line

$ tail -n+2 [Link]

Print the last N bytes.

$ tail -c8 [Link]

Print characters from the Nth byte.

$ tail -c+79 [Link]

Print last lines from dynamically changing file.


$ tail -f logfile

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.

To open the file

$ 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 [Link]

...

{Content of file}

...

...

my_file.txt (END)

To Starts up the file from the given number(less +number file_path):

$ less +4 my_file.txt

...

{Content starts from 4th line}

...

my_file.txt (END)

Keyboard shortcuts for LESS command interface

keyboard shortcuts for “less”


● [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.

● [space bar]: Next page.

● b: Previous page.

● ng: Jump to line number n. Default is the start of the file.

● nG: Jump to line number n. Default is the end of the file.

● /pattern: Search for pattern. Regular expressions can be used.

● n: Go to next match (after a successful search).

● 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.

● -option: Toggle command line option -option.

● h: Help.

● q or zz: Quit.
Forward Search Navigation

● / – search for a pattern which will take you to the next occurrence.

● n – for next match in forward

● N – for previous match in backward


Backward Search Navigation

● ? – search for a pattern which will take you to the previous occurrence.

● n – for next match in forward

● N – for previous match in backward


Line Navigation

● j – navigate forward by one line

● k – navigate backward by one line


Screen Navigation
● CTRL+D – forward half window

● CTRL+U – backward half window

● CTRL+F – forward one window

● CTRL+B – backward one window


Count numbers as options

● 5j – 5 lines forward.

● 10k – 10 lines backward.


Some more less options

● :p - Examine the previous file in the command line list.

● :d - Remove the current file from the list of files.

● v – using the configured editor edit the current file.

● &pattern – display only the matching lines, not all.

● CTRL+G – show the current file name along with line, byte and percentage statistics.

● mx – mark the current position with the letter ‘x’.

● ‘x – go to the marked position ‘x’.

Common questions

Powered by AI

To optimize the 'find' command for large-scale search operations, several strategies can be employed. Limiting depth using '-maxdepth' reduces search overhead by focusing the command on specific directory levels . Using specific search criteria, such as '-type' to differentiate files from directories, streamlines the search process by minimizing unnecessary checks . Combining '-exec' or '-print0 | xargs' enhances execution speed by applying actions only to matched files, reducing inode traversal time . Additionally, scheduling search tasks during off-peak hours when the file system load is lower helps maintain system performance. These strategies contribute to efficient large-scale file searches with minimal impact on system resources.

The 'tar' command is pivotal in Linux for creating archival files. It supports options to create, list, and extract archives. The basic syntax 'tar -cvf' creates an uncompressed archive, while 'tar -cvzf' and 'tar -cvjf' add compression using gzip and bzip2, respectively, which helps reduce storage requirements . These options allow users to manage large file collections efficiently by bundling them into a single archive, simplifying handling and transferring. Additionally, listing archive contents without extraction using options like '-tvf' helps verify files before extraction . The ability to append, update, and delete files within archives enhances flexibility for storage management tasks, making 'tar' a comprehensive tool for archival operations.

The 'chmod' command alters file or directory permissions, crucial for enforcing security policies by controlling read, write, and execute permissions . The 'chown' command changes the owner of files and directories, impacting who has administrative control over them . Improper use of 'chmod', such as setting 777 permissions indiscriminately, exposes files to unauthorized modifications and data breaches . Incorrect usage of 'chown' could inadvertently grant control to incorrect users, leading to unauthorized access or data loss . It's vital to apply these commands judiciously to maintain system security integrity and prevent accidental exposures.

File and directory compression optimizes resource management by reducing storage requirements, leading to cost savings and improved data transfer efficiency due to smaller file sizes . Compressed files, using tools like 'gzip' and 'bzip2', facilitate quicker backups and file transfers across networks, minimizing bandwidth usage. However, the trade-offs include additional CPU processing time for compression and decompression, especially with bzip2, which can impact performance . Moreover, compression can complicate file accessibility and readability, requiring decompression for use. While compression aids in efficient storage and transfer, balancing processing overhead with storage gains is essential for optimal resource management.

Cron jobs in a Linux environment are configured using scheduled entries that specify execution times and commands. Jobs are added to the crontab file using 'crontab -e', where users can specify tasks to run at precise intervals. The format consists of five time-related fields followed by the command to execute . Key considerations include ensuring specified commands have appropriate permissions and paths, as jobs run with the user's environment settings. It's also important to manage job frequency to avoid system overloads, especially with resource-intensive tasks. Logging and error handling should be implemented to catch issues early, maintaining system reliability and efficiency . Properly configured, cron jobs greatly enhance system automation and task management.

The 'find' command in Linux is versatile for searching files and directories based on various criteria. It can search specifically for files or directories using the '-type' option, such as '-type f' for files and '-type d' for directories . The command can also identify files with specific permissions using the '-perm' option. For example, 'find . -type f -perm 0664' finds files with those permissions . This capability allows administrators to locate files with incorrect permissions that could pose security issues. Additionally, the command can search within specific paths, ignore case sensitivity using '-iname', limit search depth using '-maxdepth', and combine multiple criteria with operators like '-o' for logical OR . These features make the command powerful for tailored searches.

The 'wget' command is a robust tool for automating file downloads in Linux. It supports downloading files from HTTP and FTP protocols via a single command, displaying progress and details . Wget can be used to download files under a different name using the '-O' option, enabling customization of output files . Additionally, it supports downloading multiple files simultaneously, as specified within a file containing URLs, which streamlines operations when dealing with several resources . The ability to download files using unique parameters, such as authentication or customized filenames, provides users flexibility in managing diverse download scenarios efficiently.

Directory permission settings impact access control and security within a file system. For example, a setting of 777 (rwxrwxrwx) allows any user to list, create, and delete files, which poses significant security risks as it provides unrestricted access . A more secure setting, such as 755 (rwxr-xr-x), allows the owner full access while others can only view the directory's contents, reducing the risk of unauthorized modification . Furthermore, private directories benefit from 700 (rwx------), where only the owner has any access rights, enhancing security for sensitive directories . Understanding these implications is crucial for setting permissions that balance accessibility with security to protect data.

'gzip' and 'bzip2' are both compression tools with different performance characteristics. 'gzip' is generally faster for compressing and decompressing but results in larger archive sizes compared to 'bzip2', which offers a higher compression ratio but at the cost of slower processing speeds . When determining which tool to use, considerations include the need for speed versus storage efficiency. For example, when working with large datasets where space is a premium, 'bzip2' might be preferred despite its slower operation . Conversely, for quick compressions with minimal processing overhead, 'gzip' is suitable . The decision often depends on the specific constraints and priorities of the task at hand.

When using SSH for remote login and command execution, several security considerations are paramount. Ensuring the use of strong authentication methods, such as key-based authentication, is crucial to prevent unauthorized access, as it is more secure than password-based login . Additionally, employing strong encryption algorithms helps protect data transmitted over the network from eavesdropping and interception . It's also vital to regularly update and patch SSH software to defend against known vulnerabilities. Moreover, using network-level security controls, such as allowing connections only from trusted IP addresses, mitigates the risk of unauthorized access attempts. These security practices are critical to maintaining system integrity and protecting sensitive data from unauthorized access or exposure.

You might also like