Tuesday, 1 June 2010

Basic file management commands

Command Purpose
cd Sets location in filesystem
ls Displays contents of directory
file Determines file’s type
cat Displays file’s contents
more Displays file’s contents one screen at a time
less Displays file’s contents one screen at a time
wc Shows character, word, and line counts
head Displays first few lines of a file
tail Displays last few lines of a file
touch Changes file’s timestamp; create an empty file
cp Copies a file
dd Copies a file from one device to another
mv Changes a file’s name or location in the filesystem
rm Deletes a file
mkdir Creates a directory
rmdir Deletes a directory

cd
Example:
$ cd /home/hadden/letters <---using absolute path
$ cd letters <---using relative path
$ pwd
/home/hadden/letters/Aug
$ cd
$ pwd
/home/hadden
=====================================
file
The file utility enables you to get information about the contents of a file without having to examine the file directly.
The syntax for file is
file [options] filename
Example
$ file home
home: directory
======================================

cat
The cat (concatenate file) command can be used to create new files; however, it is primarily used to send the contents of one or more files to your display or other output device. cat’s functionality can be increased by using either > or >>.
Example:
To create a new file, type
$ cat > newfilename
file contents
Ctrl-D

To display the contents of one or more files to the standard output, type
$ cat file1 file2

To combine multiple files into one, type
cat file1 file2 file3 > newfile

To add the contents of file1 to the end of file2, type
$ cat file1 >> file2
==============================
wc
The output appears as the number of lines, number of words, number of characters, and filename
Example:
$ wc /etc/passwd
33 45 1564/etc/passwd

==================================

head and tail
Like their names imply, these two commands let you look at either the beginning or end
of one or more files.
Example:
To show the first seven line of the file
$ head -7 /etc/passwd
To show the last five line of the file.
$ tail -5 /var/log/messages
if no option, default is 10 line.
=======================================
touch
touch is used to change the date and time of the last access or modification. The syntax is
touch [options] [date] filename
If the file does not exist, touch will create a new file of 0 length. If no date or time is specified, the current system time is used.
touch -d '14:24' file1
================================================

cp
The cp command copies both files and directories. The copy operation will overwrite any existing file with the same name, so be careful. To prevent this, you can use the -b (backup target file) or the -i (interactive) option.
To recursively copy one directory’s contents to another, use either the -r or -R option. This also will recursively copy the directory structure.
For example:
cp -r /home/peter /root

================================================
dd
The dd (device to device copy) is a special kind of copy utility.
example:
full hard disk copy
dd if=/dev/hdx of=/path/to/image


Restore Backup of hard disk copy
dd if=/path/to/image of=/dev/hdx
=============================

mv
The mv command is used to rename or move files to another location on the directory tree.

The syntax for mv is
mv [option] [source file] [target file]
=============================

rm
The rm command can be used to remove individual or multiple files and directories. After a file has been deleted, it is gone, so use it cautiously.
Use the -i option to require confirmation before files are deleted.
Otherwise, you might be left with a nonbootable system. The -f option will force deletion of write-protected files.

Be careful when you do this
rm -rf myfolder
=============================

mkdir
The mkdir command is used to create one or more directories. If no options are used, the parent directory must exist to create a child directory, as in the following examples
mkdir testing
mkdir testing/child

It also can create the parent and child directories in a single command by using the -p option.
mkdir -p testing/parent/child
============================

rmdir
The rmdir command will delete only empty directories.

Use the parent (-p) option to remove directory hierarchies.
rmdir -p testing/parent/child
This command deletes the child directory only. If there are more subdirectories in the parent directory, they will not be deleted.

However, if you type
rmdir -p testing/parent/*
all empty subdirectories of the directory parent will be deleted.
===================================

No comments:

Post a Comment