Basic Bash Commands in Linux
Jun 1st, 2008 | By admin | Category: Featured, LinuxThis reference is prepared for webmasters to provide basic knowledge of bash linux commands. You should know these if you are thinking to move to a dedicated server or have already one.
Mkdir – Make directories:
* Options
- -m, mode=MODE set permission mode (as in chmod), not rwxrwxrwx – umask
- -p, parents no error if existing, make parent directories as needed
- -v, verbose print a message for each created directory
- -help display this help and exit
- -version output version information and exit
* Usage
mkdir -m 777 folder1
Above code would create a directory named folder1 which can be read, executed and written by all kind of users. The first digit represents the owner, the second represents the group and the third represents other users. The number 7 represents all three types of permission ( read, write and execute), 6 stands for read and write only, 5 stands for read and execute, 4 is read only, 3 is write and execute, 2 is write only, 1 is execute only and 0 is no permissions.
Cd – Change Directories:
* Usage
cd folder1
The above command would change the current directory to directory named folder1.
cd ..
The above command would go to parent directory of current directory.
cd /
The above command would go to root directory regardless which directory you are in.
Mv – Move or Rename Directories & Files:
* Usage
move testfolder newfolder
This command would move/rename testfolder to newfolder.
move * /newdir/
The above command moves all files and directories in the current directory, including all the contents of those directories, from the current directory to the directory /newdir/ .
Pwd – Print Working Directory:
It shows you the full path to the directory you are currently in.
* Usage
pwd
It would print the following line:
/home/server/mywebsite.com/
Rm – Remove Directories:
* Usage
rm install
Cannot Remove DIRECTORY Is A Directory
If you are getting this error, just force it with this command.
rm -rf install
The above command would remove the whole directory with files and directories in.
Chmod – Change File Access Permissions
* Options
-c, –changes: like verbose but report only when a change is made
–no-preserve-root: do not treat `/’ specially (the default)
–preserve-root: fail to operate recursively on `/’
-f, –silent, –quiet: suppress most error messages
-v, verbose: output a diagnostic for every file processed
–reference=RFILE: use RFILE’s mode instead of MODE values
-R, –recursive: change files and directories recursively
–help: display this help and exit
–version: output version information and exit
* Usage
chmod 644 index.htm
The above command gives the file read/write by the owner and only read by everyone else.
Some Numerical Mod Presentations
600 read and write by owner
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody
Ls – Short Listing of Directories & Files:
* Options
-al: detailed listing of files and directories, showing permissions, ownership, size, and time and date stamp.
Just type ls on the command line, and you get the list of files and directories within your current directory.
Cp – Copy Files:
* Options
-i: with this option, you will be prompted before overwriting the file.
-dpr: copy preserving links (-d option), file attributes (-p option), and copy recursively (-r option)
* Usage
cp -dpr folder1 folder2/
The above code would copy the directory folder1 with its contents, to the directory under folder2.
folder2 has now:
anotherfile.html
folder1
Tar & Bzip2 – Archive Compression & Extraction
* Usage
tar -c folder/ | bzip2 > folder.tar.bz2
The above command compresses folder directory and the following command extracts the compressed archive.
bzip2 -dc folder.tar.bz2 | tar -x
Gpg – Encryption & Decryption of Files in Linux
* Usage
To encrypt and decrypt files you can use the following command:
gpg -c file
gpg file.gpg
Wget – Download Files from Web
* Options
-r: downloads the whole directory
-c: continues the previous download
-b: goes to background execution
-p: path to place the downloaded files
* Usage
The following command, downloads the whole directory, going to background after starting the download.
wget -r -b http://www.somedomain.com/files/
Slocate – Search & Locate files
This command builds the slocate database. It will take several minutes to complete this command.This command must be used before searching for files, however cron runs this command periodically on most systems.locate whereis Lists all files whose names contain the string “whereis”. directory.
Cat – Contents of Files
Sends file contents to standard output. This is a way to list the contents of short files to the screen.
Whereis
Report all known instances of a command.
Wc
Print byte, word, and line counts
Bg
Places the current job (or, by using the alternative form, the specified jobs) in the background, suspending its execution so that a new user prompt appears immediately. Use the jobs command to discover the identities of background jobs.
Cal – month year
Prints a calendar for the specified month of the specified year.
Clear
Clears the terminal screen.
Dmesg
Prints the messages resulting from the most recent system boot.
Fg
Brings the current job (or the specified jobs) to the foreground.
File
Determines and prints a description of the type of each specified file.
Free
Displays the amount of used and free system memory.
Ftp
Opens an FTP connection to the specified host, allowing files to be transferred. The FTP program provides subcommands for accomplishing file transfers.
* Usage
ftp hostname
Head
Prints the first several lines of each specified file.
Kill process_ids
Kills the specified processes, sends the specified processes the specified signal (given as a number or name), or prints a list of available signals.
* Usage
kill - signal process_ids
kill -l
killall program
killall - signal program
Mail
Launches a simple mail client that permits sending and receiving email messages.
Ping host
Sends an echo request via TCP/IP to the specified host. A response confirms that the host is operational.
Reboot
Reboots the system.
Shutdown
Shuts down the system after the specified number of minutes elapses (requires root privileges). The -r option causes the system to be rebooted once it has shut down.
shutdown -r minutes
Telnet
Opens a login session on the specified host.
telnet host
Top – System Resources
Prints a display of system processes that’s continually updated until the user presses the q key.
Traceroute
Uses echo requests to determine and print a network path to the host.
traceroute 1.1.1.1
Uptime
Prints the system uptime.
W
Prints the current system users.