Manage Files and Directories

View & Edit File.

#cat {filename} To view a file.
#cat >{filename} To create a new file (CTRL + D to save file).
#cat >>{filename} To add to a file (CTRL + D to save file).
#cat {filename} > {new filename} To create a file and copy to file.
#cat {filename1} {filename2} > {new filename} To create and write from multiple file.

View & Edit Directory.

#mkdir {directoryname} To create a directory.
#mkdir {directoryname} {directoryname} To create more than one directory.
#mkdir -p {directoryname1/directoryname2/directoryname3} To create parent directory with subdirectories. Parent directoryname1 within that directoryname2 within that directoryname3 will create.
#cd {directoryname} To change directory.
#cd.. To go back the previous location.
#cd To go back the root location.
#ls -l To view the files with deatils in a directory.
#ls -l | less To view page vise page (Press 'q' for back to prompt}.
#ls -a To view hidden files in a directory.

Copy Files.

#cp {sourcefile} {destinationfile}
#cp {sourcefile} {sourcefile} {destinationfile}
To copy a file to another location. Here user must give full path. If a file is located in /usr/src/test need to copy to another location /home/public user must give full path.
#cp -i {filename1} {filename2} This will overwrite filename2 with filename1.
#cp {filename} {directoryname} This will copy file to directory.

Move Files.

#mv {file} {directory} Here the file move to directory.
#mv {file1} {file2} Here the file1 will move to file 2. If the file 2 is existed rewrite on file 2. So use '-i', it will prompted eg: #mv -i {file1} {file2}.

Remove File or Directory.

#rm {file} Remove file.
#rm {file1} {file2} {file3} Remove all the file.
#rm -f {file1} Forcefully remove.
#rm -r {directoryname} Remove the contents of directory recursive.
#rm -rf It will remove all the file where u stand. Eg: if you stand in '/' it will delete all the file under it. Very Dangerous command use it wisely.

Link for File.

There are two type of link file;

Softlink: Shortcut, if the parent file deleted the softlink will not work.
Hardlink: Parent file created a same file in another location, means if the parent file deleted still hardlink will work.
#ln -s {traget filename} {symbolic filename} To create softlink.
#ln {traget filename} {destination filename} To create hardlink.

File Permission.

Linux has inherited from UNIX the concept of ownerships and permissions for files. This is basically because it was conceived as a networked system where different people would be using a variety of programs, files, etc. The big advantage that Linux has is its multi-user concept, the fact that many different people can use the same computer or that one person can use the same computer to do different jobs. That's where the system of file permissions comes in to help out in what could be a very confusing situation. Here explain some basic concepts about who owns the file and who can do what with a file.
If user run a command
#ls -l the output would be something like below.
-rw-r--r-- 1 varun user1 296637 Jul 26 21:26 Screenshot from 2013-07-26 21:26:51.png

This basically say, interpreting this from RIGHT to LEFT that the file Screenshot from 2013-07-26 21:26:51.png was created at 09:26PM on July 26 and is 296637bytes large. It belongs to the group user1. It belongs to varun in particular and it is one (1) file. Then come the file permissions symbols,

r
Read
4
w
Write
2
x
Execute
1

-rw-
r--
r--
User(u)
Group(g)
Others(o)
#chmod is a linux command that will let user to set permissions on afile. Using this command we can set/remove permissions using '+ or -' symbol. Always the permissions set in a way that user+/-permission group+/-permission other+/-permission.

Permissions can set by symbolic way and also numeric way.

Symbolic Way;
#chmod u-w g+x o+w Screenshot from 2013-07-26 21:26:51.png
Numeric Way;
#chmod 456 Screenshot from 2013-07-26 21:26:51.png

-r--r-xrw- 1 varun user1 296637 Jul 26 21:26 Screenshot from 2013-07-26 21:26:51.png
#chmod -R 456 Screenshot from 2013-07-26 21:26:51.png Permissions will apply recursively
Sticky Bit is mainly used on folders in order to avoid deletion of a folder and its content by other users though they having write permissions on the folder contents. If Sticky bit is enabled on a folder, the folder contents are deleted by only owner who created them and the root user. No one else can delete other users data in this folder(Where sticky bit is set). This is a security measure to avoid deletion of critical folders and their content(sub-folders and files), though other users have full permissions.
Symbolic way;(t,represents sticky bit)
#chmod o+t Screenshot from 2013-07-26 21:26:51.png
Numerical way (1, Sticky Bit bit as value 1)
#chmod 1456 Screenshot from 2013-07-26 21:26:51.png
When the sticky bit is set, only the item's owner, the directory's owner, or the superuser can rename or delete files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner.

Compress and Uncompress files or directory. 
Normal Compress
#tar -cvf {archivename.tar} {directoryname} To Compress
#tar -xvf {filename.tar} To uncompress

GZIP Compress
#tar -czvf {archivename.tar.gz} {directoryname} To Compress
#tar -xzvf {filename.tar.gz} To uncompress

b2z Compress
#tar -cjvf {archivename.tar.bz2} {directoryname} To Compress
#tar -xjvf {filename.tar.bz2} To uncompress

XZ Compress

#uncz {archivename.tar.xz} {directoryname} To Compress
#unxz {filename.tar.xz} To uncompress

No comments:

Post a Comment