Basic Linux commands to know system
date - display date and time of the system
pwd - display present working directory
whoami - display user
cal - display current month calander
man - display help of the commad specified. man -ls
sudo - sudo command stands for Super User Do. x`
sudo -su gives root permission
sudo -V gives version of linux
sudo -h display help
history display history of the all the commands executed during the session
uptime Display How long the server is on
Directory Commands
ls - list files
ls -a list hidden files
ls -l list the files and directories in long list format with extra information
ls -lh ls -lh /boot list the files and directories in human readable numbers format with extra information
ls *.sh list files ends with .sh
ls -d */ list all the directory
ls ../ list all the files from root directory
ls -R list the subdirectory
mkdir - create a directory
mkdir -p create nested directory mkdir ~/testdir
mkdir /home/usr/
rmdir
rmdir -r remove nested directory
rm -rf remove all files and directories
cd Used to move around in the directory/file structure.
cd .. go to the previous directory.
cd ~ go to the home directory
cd / go to the root directory.
cd ../.. go up by 2 directories.
Absolute Path and Relative Path
[root@localhost lib]# cd usr/bin
sh: cd: usr/bin: No such file or directory
[root@localhost lib]# cd /usr/bin/
[root@localhost bin]#
Practice
Display your current directory.
Change to the /etc directory.
. Go to the root directory.
List the contents of the root directory.
List the files in /boot in a human-readable format.
Create a directory testdir in your home directory.
Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2, and dir2 is a subdirectory from dir1 ).
Remove the directory testdir.
Remove dir1
Answers by Tawheed
[root@localhost /]# history
1 pwd
2 cd /etc
3 cd ..
4 ls -l
5 ls -lh /boot
6 mkdir tesdir
7 mkdir -p dir1/dir2/dir3/dir4
8 rm testdir
9 rm -rf testdir
10 rm -rf dir1
11 history
Linux is case sensitive so file.txt is different from File.txt
File commands
touch <filename> <filename> <filename> create zero size file
cat <filename> display file content
cat -b: This adds line numbers to non-blank lines
cat -n: This adds line numbers to all lines
cat -s: This squeezes blank lines into one line
cat –E: This shows $ at the end of the line
cat <file name> <filename>
cat > filename.txt create file press ctrl+d to end the file
cat filename 1 > filename2 copy file1 to file 2
file <filename> display type of file irrespective of the filename
rm <filename>/<directoryname> remove file or directory
rm -rf rm -rf (the f means force and the r means recursive)
rm -r remove recursive
rm -i provides for interactive option before delete
cp <source file/directory name> <destination filename/directory name> copy files and directory from source to destination
cp -r <source file/directory name> <destination filename/directory name> recursive copy
cp <filename> <filename> <filename> <destination directoryname> for multiple copy of files the destination must be directory
cp -i ask when file already exists to overwrite or not.
mv <source file/directory name> <destination filename/directory name> move or rename files
mv <source directory name> <destination directory name>
mv -i will ask permission to overwrite an existing file.
rename <oldname> <newname> *.txt example : rename .txt .newtxt *.txt
file <filename> display the file type
Tr: Tr stands for translation. This command is for translating or deleting characters.
cut - used to cut the characters from output
Practice
Display the content of file /etc/passwd
Create file file1.txt file2.txt file3.txt using touch
Create a directory test
Display all the created files
Remove file1.txt
Copy file2.txt and file3.txt to test/ using a single command
Copy file2.txt to newfile2.txt
Create a Directory backup and copy all the content to the backup directory
Copy all the text files to backup directory
Copy backup directory to newbackup directory
Move file2.txt to file22.txt and then move to backup directory
Rename all .txt files to .html
delete backup directory
/etc/hosts contains hostnames with their ip address
/etc/resolv.conf should contain the ip address of a DNS name server
/etc/passwd contains user password
environment variables in Linux
printenv print all environment variables.
USER - The current logged in user.
HOME - The home directory of the current user.
EDITOR - The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
SHELL - The path of the current user’s shell, such as bash or zsh.
LOGNAME - The name of the current user.
PATH - A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
LANG - The current locales settings.