Tizag.com Webmaster Tutorials - A collection of webmaster tutorials from HTML to PHP.

Thursday, August 29, 2013

Linux Quick Commands




Directory ::
pwd           current directory

Directory Listing ::
ls               list all files in current directory
ls -a           list all files in current directory includes hidden files
ls -al          list all files in current directory includes hidden files with permission, ownership and size
ls -al a*      list all files in current directory includes hidden files with permission, ownership and size matching a [Case]
ls -S          list all files in current directory sorts files by their size
ls -r           list all files in current directory sorts files in reverse order
ls -R          list all files in current directory recursively below the sub-directory
ls -F          list all files in current directory with extension 


Copy ::
cp original_file new file                            inside same directory
cp original_file /dir/dir/                             to a different directory
cp original_file /dir/dir/new_file                 to a different directory with new name
cp /dir/dir/original_file .                            from a different directory to current location

Move ::
mv filename /dir/                   move a file from current location to different location        
mv /dir/filename .                  move a file from different location to current location
mv original_file new_file         rename a file in same location


    -i (interactive) — Prompts you if the file you have selected overwrites an existing file in the destination directory.

    -f (force) — Overrides the interactive mode and moves without prompting. Be very careful about using this option.

    -v (verbose) — Shows the progress of the files as they are being moved. 



Delete ::

rm filename          deletes the file
rmdir dirname/      deletes directory [If directory is empty]
rm -rf /dir/             deletes directory and all its contents
   -i (interactive) — Prompts you to confirm the deletion.
  •    -f (force) — Overrides interactive mode and removes the file(s) without prompting.
  •    -v (verbose) — Shows the progress of the files as they are being removed.
  •    -r (recursive) — Deletes a directory and all files and subdirectories it contains.    


    find . -name foo   Finds the file foo in the current directory
    locate tree        Finds all file(s)/directory(ies) with tree. Can be tree.txt or btree
    mdfind kind:folder "farm-animal-type"

File Permission ::
rw------- (600) Only the owner has read and write permissions.
-rw-r--r-- (644) Only the owner has read and write permissions; the group and others have read only.
-rwx------ (700) Only the owner has read, write, and execute permissions.
-rwxr-xr-x (755) The owner has read, write, and execute permissions; the group and others have only read and execute.
-rwx--x--x (711) The owner has read, write, and execute permissions; the group and others have only execute.
-rw-rw-rw- (666) Everyone can read and write to the file. (Be careful with these permissions.)
-rwxrwxrwx (777) Everyone can read, write, and execute. (Again, this permissions setting can be hazardous.) 
     r — file can be read
     w — file can be written to
     x — file can be executed (if it is a program)
    - (dash) — specific permission has not been assigned

Directory Permission ::
drwx------ (700) Only the user can read, write in this directory.
drwxr-xr-x (755) Everyone can read the directory; users and groups have read and execute permissions. 
    d — a directory
    - (dash) — a regular file (rather than directory or link)
    l — a symbolic link to another program or file elsewhere on the system 

Identities
   u — the user who owns the file (that is, the owner)
   g — the group to which the user belongs
   o — others (not the owner or the owner's group)
   a — everyone or all (ug, and o)
Permissions
   r — read access
   w — write access
   x — execute access
Actions
   + — adds the permission
   - — removes the permission
   = — makes it the only permission      

chmod o+w filename.txt      tells the system you want to give others write permission to the file filename.txt
chmod go-w filename.txt   tells the system you are removing group and others write permission to the file.

    g+w — adds write access for the group
    o-rwx — removes all permissions for others
    u+x — allows the file owner to execute the file
    a+rw — allows everyone to read and write to the file
    ug+r — allows the owner and group to read the file
    g=rx — allows only the group to read and execute (not write) 


chmod +x dir  # Set a directory to be listable
chmod +x file # Set a file to be executable
To change the owner of a file/directory recursively (affecting all descendants):
chown -R username           dir # Recursively set user
chown -R username:groupname dir # Recursively set user and group
To change permissions bits of all files in a directory, recursively:
find dir -type f -exec chmod 644 {} ';' # make all files       rw-r-r-
To change permissions bits of all directories:
find dir -type d -exec chmod 755 {} ';' # make all directories rwxr-xr-x

It would be nice if you could just do this:
chmod -R 755 dir


Grep ::
Finding relevant word and exclusion of  irrelevant word.

grep Exception logfile.txt                                     look for Exception 
grep Exception logfile.txt | grep -v ERROR             -v option to exclude 
grep -c "Error" logfile.txt                                     -c option to count the word
grep --context=6 myword logfile.txt                     show additional six lines after matching 
egrep 'Error|Exception' logfile.txt                         search for either Error or Exception by executing just one command
grep Lin file1 file2 file3                                      search the three files for any line that contains the string Lin: 
grep 'Linux is' file1 file2 file3                              search for the phrase Linux is: 
grep 'Linux is' *                                                  search for the phrase Linux is: in all files in the current directory 


No comments: