2F. Day 1: How to Work on Unix System (Hands on Session) - bioinfokushwaha/Livestock_Genomics GitHub Wiki
Objective: In this exercise is to learn genomics data management for processing and analysis
I: Navigation and exploration of folders
1. To Print the name of current/working directory : pwd
$pwd
Whenever you want to know, where are you OR when you lost on unix terminal, use pwd command.
2. To list content of folder : ls
Syntax
ls [-l], [-lh] [<directory_to_list>]
-l: Listing of files and folders
-h: Human readable format
-a: Do not ignore entries starting with '.' i.e hidden files
-r: Reverse order while sorting
-R: List sub-directories recursively
-s: Print the allocated size of each file, in block
-S: Sort by file size, largest first
-t: Sort by modification time, newest first (to see last updated files)
File permissions, ownership, size and date of creation, you can add the -l flag to the command.
Use wild-card to list file and folder starting with same alphabet.
$ ls
$ ls -lh
3. Make directory: mkdir
Syntax
mkdir <folder_name>
-m: Set folder mode (as in chmod), not a=rwx - umask
-p: Make parent directories as needed
-v: Explain what is being done (Print a message for each created directory)
mkdir simply creates a new empty folder.
$mkdir –p my_directory
$mkdir –pv my_directory
$mkdir -m a=r+w my_directory (To give read and write permission to folder )
4. Change directory: cd
Syntax
cd <target_directory>
Examples
$ pwd
/ home / reaper
$ cd Documents
$ pwd
/ home / reaper / Documents
5. Create a file: nano
Command usage: nano <file_name>
1. nano My_nanofile.txt
2. write something “This is testing of nano text editor"
3. <Control> + o (To save the file )
4. Enter
5. <Control> + x (Exit the file )
II: Text reading and redirection of text file
1. Display file content: cat/less/more
cat filename
less filename
more filename
2.To see top content of file : head
syntax
head [option] file_name
Option
-n : to show the specified number of lines
-c : to show the specified number of bytes
-v : to show the file name tag
-q : to don't separate the content of multiple files with a file name tag
Examples
head filename
head -n filename
3.To see bottom content of file :tail
syntax
tail [option] file_name
Option
-n : to show the specified number of lines
-c : to show the specified number of bytes
-v : to show the file name tag
-q : to don't separate the content of multiple files with a file name tag
Examples
tail filename
tail -n filename
III: Copying and Removing Files
1. Copy files and directories
Syntax
Command usage: cp [-i] <target_file> <end_location>
options:
-i: Prompt before overwrite
-f: If an existing destination file, remove it and try again
-u, Update copy only when the source file is newer than the destination file or when the destination file is missing
-t: Copy all source arguments into directory
-r: Copy directories recursively
-n: Do not overwrite an existing file (overrides a previous -i option)
Examples
$cp -i xaa_file Sandeep/
cp: overwrite 'Sandeep/xaa'? (if file exist)
$cp xaa_file Sandeep/
2. Move/rename file : Used for two different purposes: Move a file to a different location/Rename a filename
Syntax
Command usage: mv (-i) <target_file> <end_location>
options:
-i: Prompt before overwrite
-f: Pf an existing destination file, remove it and try again
-u: Update copy only when the source file is newer than the destination file or when the destination file is missing
-r: Copy directories recursively
-n: Do not overwrite an existing file
One useful flag argument when moving, copying or removing files is the -i (interactive) flag. If this flag is used, you will be asked if you want to remove files before they are permanently erase.
Examples
$ mv -i File.txt my_directory/File_renamed.txt
mv : overwrite ’ Docs /File_renamed.txt ’?
# You can reply to this by typing ’y ’ (YES) or ’n ’ (NO) , and then return
$ mv File1.txt my_directory/File1_renamed_file.txt
# Note : No warning here and file will be overwritten if exist.
3. Remove directory and files:rm (-f/-i/-r/-v)
Syntax
rm [OPTION]... [FILE]...
-f: Delete files forcefully, ignore nonexistent files and arguments, never prompt
-i: Prompt before every removal
-r: Remove directories and their contents recursively
-d: Remove empty directories
-v: Explain what is being done
Examples
$ rm –irv my_directory
$ rm –irvf my_directory
$ rm –irvf file_name
IV. File Streaming
File streams: Standard input (stdin): Used to read file content into a program.
echo " Adding a line " > a_short_file . txt
echo " Adding another line " >> a_short_file . txt
Standard output (stdout): Used to output content from the program.
$ echo " More text ! " > for_later_usage . txt ## save in first/different file
$ echo " More text ! " >> for_later_usage . txt ## save in same file
Standard error (stderr): Similar to stdout, but commonly used to separate error messages from regular program output.
large_program > stdout_example.txt 2> stderr_example.txt
V. Navigation of files and folders
Directories
Present working directories: pwd
Special directories:
Root directory (/): highest directory in system
Home directory(~): home directory of user
Current directory(.): where you are
Parent directory (..): one directory before current directory
Path
Absolute path: i.e start from root/home directories
Relative path: i.e. start from current/parent directories
VI: Compress and decompress
Zip
Gzip [c,-k]
tar [-czvf] compressed_file_name Source_file_name
bzip2