Handy Linux Commands
Posted by Help Desk (6), Last modified by NOC Desk Monitors on 07 August 2018 11:41 AM

Note: Not all commands work on all flavours of Linux.

Further information on a command can be found quickly with 'command --help' or 'man commandname' for the manual.



Navigation:

/  (refers to the root directory on the server)
./  (the current directory that you are in)
../  (parent directory of your current directory)

pwd  (shows what you current directory is - giving the full path)

ls  (lists all the files in your current directory)
ls -l (lists filenames 1 per line in long format)
ls -al  (lists filenames and hidden files + information)
ls -alR  (lists filenames + information in all subdirectories)ls -alR | more (lists filenames + information in all subdirectories, pausing when the screen become full)
ls -alR > result.txt  (lists filenames + information in all subdirectories, and ouputs the results to a file instead of the screen)
ls *.html  (lists all files ending with .html)
ls -al /home/usr/bob/  (lists files + info for /home/usr/bob)
ls -lh --sort=size | less (list files a page at a time by size order with the highest file size in a human readable format KB/MB etc)
ls -lh --sort=time | less (list files a page at a time by time order with newest first)
ls | wc -l (counts the number of files in the folder you are in)

cd  directoryname (changes you to a new directory)
cd /  (changes you to the root directory)
cd /directoryname (changes you to a directory at root level no matter how many levels in to a directory you are)
cd ..  (this goes back one directory)

cd ../..  (this goes back two directories)



Moving, Copying and Deleting Files:

mv oldname newname  (move/rename a file)

cp filename newfilename  (copy a file)
cp filename /directory/newfilename (copy a file to another directory)

rm filename  (delete a file)
rm *  (delete all files in your current directory)
rm *.html  (delete all files ending in .html in your current directory)
rm -f *.html (delete all files ending in .html in your current directory without being prompted for y/n)



Creating, Moving, Copying and Deleting Directories:

mkdir directoryname  (creates a new directory)

ls -d */  (lists all directories within current directory)

cp -r directoryname newdirectoryname  (copy a directory and all files/directories in it)

rmdir directoryname  (remove a directory if it is empty)
rm -r directoryname  (remove a directory and all files and sub directories in it)
rm -rf directoryname (remove a directory and all files and sub directories in it without being prompted y/n)



Searching Files and Directories

find / -name filename -print  (search the whole server for a file)
find . -name filename -print  (search for a file starting with the current directory)
find / -name directoryname - type d -print  (search the whole server for a direcory)

updatedb (update the slocate database)
locate filename (searches the whole server for filename - need to 'updatedb' prior to searching for new files)

grep text filename  (search for text within a file)

sed s/oldtext/newtext/g filename  (searches file and replaces all occurances of oldtext with newtext

du -sh * (lists disk usage of all files and directories from the directory you are in)



Compressing/Uncompressing Files and Directories

tar -zxvf filename.tar.gz   (uncompresses tar.gz files)
tar -zcvf fielname.tar.gz directoryname  (compresses the directory t oa tar.gz.file)

gunzip filename.gzip   (uncompresses gzip files)

unzip filename.zip   (uncompresses zip files)



File and Directory Permissions

There are three levels of file permission: read, write and execute. In addition, there are three
groups to which you can assign permission, The file owner, the user group, and everyone. The command chmod followed by three numbers is used to change permissons. The first number is
the permission for the owner, the second for the group and the third for everyone. Here are how
the levels of permission translate:

0 = --- (no permission)
1 = --x (execute only)
2 = -w- (write only)
3 = -wx (write and execute)
4 = r-- (read only)
5 = r-x (read and execute)
6 = rw- (read and write)
7 = rwx (read, write and execute)

Here are the most common file permissions used:

chmod 604 filename (minimum permission for www HTML file)
chmod 705 directoryname (minimum permission for www directories)
chmod 705 filename (minimum permission for www scripts & programs)
chmod 703 directoryname (write-only permission for public FTP uploading)
chmod 755 filename (permission for CGI scripts)

 

 

(4 vote(s))
Helpful
Not helpful