Saturday, March 29, 2014

Linux basic commands

Basic file manipulation

Pwd = show Present Working Directory
Example: root@localhost:~#pwd
/root

man <command> = shows manual


ls = lists contents of current directory


[davr@localhost ~]$ ls
2  abc        dir1       err      file2     if.sh    Music     Templates
3  davron.sh  Documents  err.txt  hello.sh  ls.txt   Pictures  test
5  Desktop    Downloads  file1    ifq       math.sh  Public    Videos

ls –l = lists contents of current directory with extra details

ls -a = list all files and folders with hidden files
ls <folderName> = list files in folder
ls -lh = Detailed list, Human readable
ls -l *.jpg = list jpeg files only mv file.txt Documents/ = move file to a folder
ls -lh <fileName> = Result for file only


cd <folderName> = change directory 
cd /   = go to root
cd ..  = go up one folder, tip: ../../../ 
cd ~ =change directory to your home directory 
cd /  =scratch/user change directory to user on scratch 
cd -  =change directory to the last directory you were in before changing to wherever you are now.

 mkdir = create new folder

touch <fileName> = create or update a file
rm myfile removes file called myfile 
rm –f myfile removes myfile without asking you for confirmation. useful if using wildcards to remove files *** 
rm <fileName> .. = delete file (s)
rm -i <fileName> ..k = ask for confirmation each file 
rm -r <dirname> = removes the direcory 
rm –rf mydir this will delete directory mydir along with all its content without asking you for confirmation!

nano opens a text editor. see ribbon at bottom for help. ^x means CTRL-x. this will exit nano 
nano new.txt opens nano editing a file called new.txt 


cat <fileName> = show content of file (less, more)

head new.txt displays first 10 lines of new.txt 
tail new.txt displays last 10 lines of new.txt 
tail –f new.txt displays the contents of a file as it grows, starting with the last 10 lines. ctrl-c to quit. 

mv <folderName> <folderName2> = move folder in folder

mv filename.txt filename2.txt = rename file. (this will overwrite it!)
mv file.txt Documents/ = move file to a folder
mv <folderName>/ .. = move folder up in hierarchy 
mv dir subdir moves the directory called dir to the directory called subdir 
mv dir newdirname renames directory dir to newdirname  

cp image.jpg newimage.jpg = copy and rename a file

cp image.jpg <folderName>/ = copy to folder
cp image.jpg folder/sameImageNewName.jpg
cp -R stuff otherStuff = copy and rename a folder
cp *.txt stuff/ = copy all of *<file type> to folder

top = displays all the processes running on the machine, and shows available resources 
wc= word count
wc=<filename>=nbr of line, nbr of words, byte size -l (lines), -w (words), -c(byte size), -m(number of characters)

du -h: Disk usage of folders, human readable

du -ah: “ “ “ files & folders, Human readable
du -sh: only show disc usage of folders

find = the best file research tool 

find =  -name "filename"
find =  -name "text"=search for files who start with the word text

locate <text> = search the content of all the files

locate <fileName> = search for a file 
sudo updatedb = update database of files 

grep <someText> <fileName> = search for text in file

-i = Doesn't consider uppercase words
 -I = exclude binary files
grep -r <text> <folderName>/ = search for file names with occurrence of the text

With regular expressions: 

grep -E ^<text> <fileName> = search start of lines with the word text
grep -E <0-4> <fileName> =shows lines containing numbers 0-4
grep -E <a-zA-Z> <fileName> = retrieve all lines 
with alphabetical letters

sort = sort the content of files
sort <fileName> = sort alphabetically
sort -o <file> <outputFile> = write result to a file
sort -r <fileName> = sort in reverse
sort -R <fileName> = sort randomly
sort -n <fileName> = sort numbers

cut = cut a part of a file
-c --> ex: cut -c 2-5 names.txt 
(cut the characters 2 to 5 of each line)
-d (delimiter) (-d & -f good for .csv files)
-f (# of field to cut)

tar -zxf archive.tgz = this will extract the contents of the archive called archive.tgz. kind of like unzipping a zipfile. *** 
tar -zcf dir.tgz dir = this creates a compressed archive called dir.tgz that contains all the files and directory structure of dir 



Basic Terminal Shortcuts

CTRL L = Clear the terminal 

CTRL D = Logout
SHIFT Page Up/Down = Go up/down the terminal
CTRL A = Cursor to start of line 
CTRL E = Cursor the end of line
CTRL U = Delete left of the cursor 
CTRL K = Delete right of the cursor 
CTRL W = Delete word on the left 
CTRL Y = Paste (after CTRL U,K or W) 
TAB = auto completion of file or command 
CTRL R = reverse search history
!! = repeat last command
CTRL Z = stops the current command (resume with fg in foreground or bg in background)


1 comment:

  1. Linux commands

    # top - to check the prossess in a system, CPU consuming processes and they list

    # ps -ef - to check how many processes are running in a system

    # ps -ef |grep - to check certain process

    # free - to check memory usage

    # free -m - memory usage by MB

    # df -h - to check storage info and storage disk usage

    # dmidecode - to check hardware

    # uname -a - to check what is your system version, kernel version date installed, architect

    # uname -r - to check kernel version

    # arch - to check the system version

    # uptime - to check users and how many and how long have been up and login , and load average

    # umask - check te file permissions

    # cat /proc/cpuinfo - to show info about CPU

    # cat /proc/meminfo - to show info about memory in advanced way

    # du -sh - to check file or directory size

    # pmap - to check how much memory is using certain process

    # lsmod - to check list of modules

    # modinfo - to check info about modules

    # modprobe - to load the module (driver)

    # modprobe -r - to remove the module (driver)

    # partprobe - p

    # sysctl - to check kernel parameters

    #vmstat - to check virtual memory info and statistics

    #l vmdiskscan - to check physical and logical volumes and, hard drive sizes in a system

    # fdisk -l - to check hard drive sizes and partitions

    # cat /proc/partitions - to check hard drive sizes and partitions

    # parted -l - to check hard drive sizes and partitions

    # free -m - to check memory size and usage

    #mkswap - to create swap files

    # swapon - to load the swap memory

    # swapof - to unload swap memory

    # mkinitrd - to create initialized files

    # init - to execute runlevels

    # runlevel - to check runing levels in the system

    # who -r - to check runing levels in the system

    # ifconfig - to check ip addresses of the interface

    # ip addr show - to check ip addresses of the interface

    # nmap - to check open ports in the system

    # netstat -r - to check default gateways and network connections

    # route - to check default gateways and network connections

    # ip rout show - to check default gateways and network connections

    # netstat -anp| grep - to check port is open or not

    # ethtool - to check network connectivity

    # strace -o - to check the system internal processes

    # iostat - info about all the system performances

    # getenforce - to check default selinux in the system

    # setenfors 0 - to make selinux permissive

    # setenforce 1 - to make selinux enforcing



    ReplyDelete