Wednesday, November 28, 2012

File/Directory Basic commands



 File/Directory Basic commands

cat command
     cat linux command concatenates files and print it on the standard output.
Syntax  The Syntax is
     

cat [options] [Filenames]
Options
-A
Show all.
-b
Omits line numbers for blank space in the output.
-e
A $ character will be printed at the end of each line prior to a new line.
-E
Displays a $ (dollar sign) at the end of each line.
-n
Line numbers for all the output lines.
-s
If the output has multiple empty lines it replaces it with one empty line.
-T
Displays the tab characters in the output.
-v
Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly.
Example
1.     To Create a new file:
cat > file1.txt
This command creates a new file file1.txt. After typing into the file press ctrl+d (^d) simultaneously to end the file.
2.     To Append data into the file:
cat >> file1.txt
To append data into the same file use append operator >> to write into the file, else the file will be overwritten (i.e., all of its contents will be erased).
3.     To display a file:
cat file1.txt
This command displays the data in the file.
4.     To concatenate several files and display:
cat file1.txt file2.txt
The above cat command will concatenate the two files (file1.txt and file2.txt) and it will display the output in the screen. Sometimes the output may not fit the monitor screen. In such situation you can print those files in a new file or display the file using less command.
cat file1.txt file2.txt | less
5.     To concatenate several files and to transfer the output to another file.
cat file1.txt file2.txt > file3.txt
In the above example the output is redirected to new file file3.txt. The cat command will create new file file3.txt and store the concatenated output into file3.txt.
cd command  
   cd command is used to change the directory.

SYNTAX:
  The Syntax is
     cd [directory | ~ | ./ | ../ | - ]
Options     
-L
Use the physical directory structure.
-P
Forces symbolic links.
Example
1.     cd /home/data
This command will take you to /home/data
2.     cd ..
This will change to the parent-directory from the current working directory/sub-directory.
3.     cd ~
This command will move to the user's home directory which is "/home/username".
ls command     
ls command lists the files and directories under current working directory.
Syntax
  The Syntax is
     ls [options] [Filenames]
Options
-l
Lists all the files, directories and their mode, Number of links, owner of the file, file size, Modified date and time and filename.
-t
Lists in order of last modification time.
-a
Lists all entries including hidden files.
-d
Lists directory files instead of contents.
-p
Puts slash at the end of each directories.
-u
List in order of last access time.
-i
Display inode information.
-ltr
List files order by date.
-lSr
List files order by file size.
Example    
Display root directory contents:
ls /
lists the contents of root directory.
1.     Display hidden files and directories: 
ls -a
lists all entries including hidden files and directories.
cp command
     cp command copy files from one location to another. If the destination is an existing file, then the file is overwritten; if the destination is an existing directory, the file is copied into the directory (the directory is not overwritten).
Syntax
  The Syntax is
     cp [options]... sourcefile destination file
     cp [options]... sourcedir destinationdir
Options     
-a
same as -dpR.
--backup[=CONTROL]
make a backup of each existing destination file
-b
like --backup but does not accept an argument.
-f
if an existing destination file cannot be opened, remove it and try again.
-p
same as --preserve=mode,ownership,timestamps.
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps) and security contexts, if possible additional attributes: links, all.
--no-preserve=ATTR_LIST
don't preserve the specified attribute.
--parents
append source path to DIRECTORY.
Example     
1.     Copy two files:
cp file1 file2
The above cp command copies the content of file1.php to file2.php.
2.     To backup the copied file:
cp -b file1.docl file2.doc
Backup of file1.docl  will be created with '~' symbol as file1.doc2~.
3.     Copy folder and subfolders:
cp -R data data2
The above cp command copy the folder and subfolders from data to data2
mv command
     mv command which is short for move. It is used to move/rename file from one directory to another. mv command is different from cp command as it completely removes the file from the source and moves to the directory specified, where cp command just copies the content from one file to another.
Syntax
  The Syntax is
     mv [-f] [-i] oldname newname
Options
-f
mv -f will move the file(s) without prompting even if it is writing over an existing target.
-i
Prompts before overwriting another file.
Example     
1.     To Rename / Move a file:
mv file1.txt file2.txt
This command renames file1.txt as file2.txt
2.     To move a directory
mv data  tmp
In the above line mv command moves all the files, directories and sub-directories from data folder/directory to tmp directory if the tmp directory already exists. If there is no tmp directory it rename's the data directory as tmp directory.
3.     To Move multiple files/More files into another directory 
mv file1.txt tmp/file2.txt newdir
This command moves the files file1.txt from the current directory and file2.txt from the tmp folder/directory to newdir.
rm command
     rm linux command is used to remove/delete the file from the directory.
Syntax
  The Syntax is
     rm [options..] [file | directory]
Options
-f
Remove all files in a directory without prompting the user.
-i
Interactive. With this option, rm prompts for confirmation before removing any files.
-r (or) -R
Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed. The user is normally prompted for removal of any write-protected files which the directory contains.
Example     
1.  To Remove / Delete a file:
rm file1.txt
Here rm command will remove/delete the file file1.txt.
2.  To delete a directory tree:
rm -ir tmp
    This rm command recursively removes the contents of all subdirectories of the tmp directory, prompting you regarding the removal of each file, and then removes the tmp directory itself.
3.  To remove more files at once
rm file1.txt file2.txt
rm command removes file1.txt and file2.txt files at the same time.
mkdir command
     mkdir command is used to create one or more directories.
Syntax
  The Syntax is
     mkdir [options] [directoriename]
Options
-m
Set the access mode for the new directories.
-P
Create multiple directories
-v
Print help message for each directory created.
Example
1.     Create directory: 
mkdir data
The above command is used to create the directory 'data'.
2.     Create directory and set permissions:
mkdir -m 666 data
The above command is used to create the directory 'data' and set the read and write permission.


Stay Connected With Free Updates!

Subscribe via Email

Follow me!

Leave Your Comment Below If You Like This Post

0 comments: