Sunday, 6 April 2014

Linux Filesystem Commands

2 comments
Linux Filesystem simplified, Linux Filesystem Commands
df [options] [device name] : It displays the filesystem usage related information
-a : Displays all the filesystems
-i : Gives inode usage information
-h : Displays in human readable format. Shows quantified byte information

Example -:
$ df -a
Filesystem       1K-blocks    Used Available Use% Mounted on
/dev/sda1          7608792 2469924   4729320  35% /

proc                     0       0         0    - /proc
sysfs                    0       0         0    - /sys
none                     0       0         0    - /sys/fs/fuse/connections
none                     0       0         0    - /sys/kernel/debug
none                     0       0         0    - /sys/kernel/security
udev                244408       4    244404   1% /dev
devpts                   0       0         0    - /dev/pts
tmpfs               101288     772    100516   1% /run
none                  5120       0      5120   0% /run/lock
none                253216     124    253092   1% /run/shm
gvfs-fuse-daemon         0       0         0    - /home/mahesh/.gvfs
/dev/sr0             62658   62658         0 100% /media/VBOXADDITIONS_4.3.6_
91406  
du [options] [pattern]  :  Displays space usage on files and directories
-c : Displays grand total for all the arguments
-h : Displays in human readable format. Shows quantified byte information
Example -:
$ du -ahc
0 ./centos/6
8.0K ./centos
4.0K ./samplefile.txt
0 ./ubuntu/ub10
4.0K ./ubuntu
16K .
16K total

ls [options] [filepattern] : Lists out directories and file entries from the given pattern
-a : Displays all the files including . &  ..
-r : Lists all the files in directories recursively
-l : Displays long list consisting of permissions on each file and other details
-d : Lists directories and not their content
-x : Displays sorted list by extension of the file
-s : Sorts output according to file size
-u : Displays sorted list by the access time
Example -:
$ ls -la
total 160
drwxr-xr-x 20 mahesh mahesh  4096 Mar 24 00:00 .
drwxr-xr-x  3 root   root    4096 Mar  9 01:23 ..
-rw-r--r--  1 mahesh mahesh   220 Mar  9 01:23 .bash_logout
-rw-r--r--  1 mahesh mahesh  3486 Mar  9 01:23 .bashrc
drwx------ 14 mahesh mahesh  4096 Mar 24 00:00 .cache
drwx------ 10 mahesh mahesh  4096 Mar 24 00:11 .config
drwx------  3 mahesh mahesh  4096 Mar  9 01:36 .dbus
drwxr-xr-x  2 mahesh mahesh  4096 Mar 24 00:11 Desktop
-rw-r--r--  1 mahesh mahesh    25 Mar 23 19:50 .dmrc
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Documents
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Downloads
-rw-r--r--  1 mahesh mahesh  8445 Mar  9 01:23 examples.desktop
drwx------  3 mahesh mahesh  4096 Mar 23 19:51 .gconf
drwx------  4 mahesh mahesh  4096 Mar  9 01:37 .gnome2
-rw-rw-r--  1 mahesh mahesh   142 Mar 23 19:51 .gtk-bookmarks
dr-x------  2 mahesh mahesh     0 Mar 23 19:51 .gvfs
-rw-------  1 mahesh mahesh   724 Mar 23 19:51 .ICEauthority
drwxr-xr-x  3 mahesh mahesh  4096 Mar  9 01:36 .local
drwx------  3 mahesh mahesh  4096 Mar  9 01:37 .mission-control
drwx------  4 mahesh mahesh  4096 Mar 24 00:00 .mozilla
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Music
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Pictures
-rw-r--r--  1 mahesh mahesh   675 Mar  9 01:23 .profile
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Public
drwx------  2 mahesh mahesh  4096 Mar 23 19:51 .pulse
-rw-------  1 mahesh mahesh   256 Mar  9 01:36 .pulse-cookie
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Templates
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Videos
-rw-------  1 mahesh mahesh    62 Mar 23 19:50 .Xauthority
-rw-------  1 mahesh mahesh 23406 Mar 24 02:44 .xsession-errors
-rw-------  1 mahesh mahesh 12494 Mar 16 15:11 .xsession-errors.old 

mkdir [options] [directory-name] : Creates a new directory by the given directory-name
-m : Sets file mode
-p  : Creates parent directories if it does not exists.
Example -:
$ mkdir testdir
The above command will create a directory named "testdir"

 touch [options] [pattern] : Updates the timestamp of a file, If the file does not exists by default options creates it.
-a : Changes the access time only
-c : Do not create the file
-t  : Give a timestamp to use instead of current time
Example -:
$ touch testfile.txt
Creates "testfile.txt" if it does not exists, else it only updates timestamp of the "testfile.txt".

shred [options] [file_pattern] : Enables to overwrite a file to hide its content, also allows to delete file data in secured manner.
-s : This followed by number of bytes, will shred those many bytes
-n : Number of pattern iterations to run
-z : Add a final overwrite with zero to hide shredding
-u : Truncate and remove the file after overwriting
 Example -:
$ shred file1.txt file2.txt
The above command will destroy file1.txt and file2.txt completely, so not be able to recover even using any recovery utilities

rm [options] [file_pattern] : Remove the file
-f : Force removal
-i : Prompt before removal of each file 
-r : Deletes directories and their contents recursively.
Example -:
$ rm -rf testdir
Removes forcefully and recursively all the files in "testdir" as well as removes "testdir" directory.

mv [options] [from_pattern] [to_file] : Move or Rename a file
-f  : Do not prompt before overwriting
-i  : Interactive, prompts before moving
-n : Do not overwrite an existing file
Example -:
$ mv old_name new_name
Above command renames the "old_name" file/directory to "new_name".
$ mv alphonso basket/mango
Moves a file to another directory and rename it:

tar [options] [tar_file] [pattern] : Creates, Extracts an archive.
-c  : Creates a new tar archive
-f  : Specify an tar archive file to use
-t  : List the contents
-v : Verbose mode
-x : Extracts the archive contents
-z : Compress/Decompress through gzip
-j : Compress/Decompress through bzip2
Example -:
$ tar -cvf techdarting-04-14.tar /home/techdarting/
/home/techdarting/
/home/techdarting/bass.sh
/home/techdarting/openmpi-2.1.4.tar.gz
/home/techdarting/toolkit_pram.rpm

cd [directory] : Change directory

chown [options]  [mode] [file_name] : Enables to change the ownership of a directory, file etc.
-R : Recursively change ownership.

chmod [options] [user] [file_name] :  Enables to change the permission of a file, directory, etc.
-R : Recursively change permissions

2 comments:

  1. Hi...

    you may add the following commands which are feel are frequently used (I personally use these commands quite often) :
    1)tail command
    2)cat command
    3)ps command to get the status of a process.
    4)grep command used in combination with ps command with chaining (|) to kill processes.
    5)make command which is used for building the source which is mostly used after unzipping source files from tar/gzip files...

    ReplyDelete
    Replies
    1. Thanks for your response Jatin,
      grep operates on strings, make helps in compilation and related stuff, and ps helps in getting process status,. this post specifically wanted to highlight only commands related to filesystem handling, so I didn't considered them.
      But I think "tail" & "cat" deserves to be added to this list, also I missed "find" command which I think need a place here. Thanks for pointing this out.

      Delete