Tuesday, 12 June 2018

Tutorial :: Basic commands in linux

Tags

ls Commands (list) :
ls : list the files
ls -a : list the hidden files
ls -al : list the hidden files with long formatting
ls -R : list the recursive directory tree
ls -t : list the files with time
ls -s : list the file size
ls -i : list the file size with index number
ls -ls : size with long format
ls -X : extension name
ls -S : sort by file size
cd Command (change directory) :
cd / : move to root directory
cd ~ : move to home directory
cd .. : go back one step
cd ~/directory : jump from one directory to another directly
cd /root/directory : absolute path of directory
cd directory : Relative path of directory
cd dir1/dir2 : moving directly to dir2 inside the dir1.
cd : move directly to the home directory
cd ~prakash : takes to other user login
pwd Commands (print working directory) :
pwd : print the working directory
/bin/pwd –version : print the version of pwd command
type -a pwd : print all the locations containing executable named pwd
PS1=’pwd>’ : change current working directory to anything
nano pwd.sh : create a shell script
chmod pwd.sh : give executive permission
mkdir commands (make directory):
mkdir dirname : create a new directory
mkdir -m a=rwx dirname : create a new directory and set permissions
mkdir -p /root/mine/a/b : create a specified intermediate directories to verify this “du mine”
mkdir –version : shows the version of the installed mkdir
mkdir -m 777 mine : create a new directory and set the permission as per numbers
rm commands(remove) :
rm file1 : delete the file1
rm -r dir1 : delete the dir1
rm -f file1 : delete the file1
rm -rf dir1 : force remove the dir1
rm file1 file2 file3 : delete the 3 files simultaneously
rmdir dir1 dir2 dir3 : delete the 3 directories simultaneously
rm -ri dir1 : delete file and sub directories interactively(-ri option in rm cmd)
rm -f *.txt : delete all the .txt file
cp commands (copy) :
cp file1 file2 : copy file1 to file2
cp -r dir1 dir2 : copy dir1 to dir2
cp *.txt dir1 : copy and move all the .txt files to dir1
cp -f file.txt dir1 : force file copy to dir1
cp -i file.txt dir1 : interactive prompt before file overwrite
cp -u * dirname : copy all the files to the directory
mv command (move) :
mv file1 file2 : rename the file1 to file2
mv file1 dir1 : move the file1 to the destination directory
mv file1 ../ : move the file1 to the root directory
mv *.txt dir 1 : move the all .txt files to dir1
mv dir1/* . : move all files in the subdirectory dir1 to current directory
mv dir1 dir2 : move the dir1 to dir2
ln Commands (link) :
ln -s file1 dirname : create a symbolic link “link” to file
ln -s file1 file2 : create a symbolic link to a files
ln -s -b file1.txt file2.txt : file1.txt is renamed to file2.txt , file2.txt symlink is created
touch command :
touch file1 : create a new file1
touch file1 file2 file3 : create multiple files simultaneously
touch -a test.txt : change file access and modification
touch -c test.txt : avoid creating new file
touch -m test.txt : change file modification time
touch {A..Z} : create file with names A to Z
touch {1..20 : Create files with name 1 to 10
touch {A..N}.txt : create file with extension
touch {1..10}{1..1000} : create 10k files
touch -c -t 12101730 test.txt : set the access and modification times
touch -r file leena : use time stamp of another file
touch -d ’14:24′ abc.txt ” : specify datetime as a string
cat command (concatenate) :
ct /etc/shells : display all the shell commands
cat /etc/passwd : display contents of file
cat file file1 : view contents of multiple files
cat >test1 : create a file with cat command
cat file.txt | more : use cat command with more option
cat file.txt | less : use cat command with less option
cat -n file.txt : display numbers in file
cat -e file : display $ at the EOF
cat file1; file2 file3; : diplay multiple file contents simultaneously more command :
more command :
more +3 file1.txt : display the contents of file1.txt , beginning at line 3
more +/”hope” file : display the content of the file , beginning at the first line containing the string “hope”.
ls | more : List the contents of the current directory with ls, using more to display the list one screen at a time.
head commands :
head test : print the first 10 lines from the files
head -n2 test : print the first N lines from the file
head -n -2 test : Skip the last N lines from the file
head -c 5 test : print the first N bytes from the file
head -c -7 test : to skip printing last N bytes
head -n 10 test | tail -5 : Print the lines b/w the line numbers M and N
head -n 2 /etc/passwd : print the first 2 lines of a file
head -n -27 /etc/passwd : it printed all the lines starting after the line 27
tail commands :
tail test : print the last 10 lines of the file
tail test -n 100 : Outputs the last 100 lines of the file myfile.txt
tail -n 2 /etc/passwd : print the last 2 lines of a file
tail -n-2 test : Print the last N lines from the file
tail -f test : output the contents of file as it grows, starting with the last 10 lines Process Management .
tail -c-7 test : print the last N bytes
tail -n10 test | head -5 : Print the lines b/w the line numbers M and N
tail -n -27 /etc/passwd : it printed all the lines starting before the line 27


EmoticonEmoticon