Sunday, August 17, 2014

Linux Commands For A Beginner

These are a few Linux commands for a beginner or a newbie. This may help out someone who is trying to quickly learn some basic commands in Linux world. All the best!!!

New free course from Red Hat......

Learn practical techniques for using Red Hat® Enterprise Linux® and how to perform basic system administration tasks. Check out the below web link:

View/List Files/Folders“ls” OR “ll”

“ls -ali” (view all including hidden files)

“ls -ld” (view only folders)

“ls -Zl” (view SELinux context along with file attributes)
Copy Files/Folders“cp” {cp <Options> <Source> <Destination>}
Secure Copy“scp” {scp <User@Host:SourcePath> <User@Host:DestinationPath>}

- Check manual page of scp for further options.
Move Files/Folders“mv” {mv <Options> <Source> <Destination>}
Create a New File“touch <Filename>” {creates an empty file}
“vim <Filename> {allows editing new file}
Create a Directory“mkdir <DirectoryName>”
“mkdir -p <PathOfTheDirectory>” {this would create all the directories/sub-directories in the given path if not available}
View a File“cat”

#cat <FileName>
Change Directory“cd <DirectoryName>” {directory name could be an absolute path}

“cd -" {to switch to the previous directory}

“cd ~” {to go to user home directory}

“cd ..” {to go back to parent directory OR scroll up in the directory list}
Print Current Working Directory“pwd”
Delete a File/Folder“rm <File/FolderName>”

“rm -rf <File/FolderName>” {this would wipe out a file/folder and doesn’t prompt for confirmation}
Check File Type“file” {file <File/FolderName> }
Change File/Folder Permissions“chmod”
Ex:#chmod o+w /testfile (sets write permission for this file for other users)

#chmod 646 /testfile
Change File/Folder Ownership“chown”

Ex:#chown redhat /testfile (changes file owner as redhat user)

Ex:#chown redhat:redhat /testfile (changes owner and group to redhat user for this file)
View File Special Attributes"lsattr"
Set File Special Attributes“chattr”

Ex:#chattr +i /testfile (this would not allow file deletion even by root user)

#chattr -i /testfile (this removes “immutable flag” set on the file)
View ACL attributes
(Access Control List)
“getfacl <File/FolderName>”
Set ACL attributes
(Access Control List)
“setfacl -m u:<Username>:<Permissions> <File/FolderPath>”

Ex: #setfacl -m u:testuser:rw /etc/fstab
Check Kernel Version“uname” {#uname -a Or #uname -r}
Check Redhat Release Version#cat /etc/redhat-release
Check OS Architecture#arch
#uname -m
List of PCI Devices Found#lspci
List of USB Devices Found#lsusb
List of Hard Drives Found“fdisk” {#fdisk -l}
“parted” {#parted --list}
#cat /proc/partitions
List of Processors Found“lscpu”
#cat /proc/cpuinfo
#dmidecode --type processor
List of Memory Modules Found#dmidecode --type memory
List HAL Devices Found#lshal
List Block Devices Found“lsblk”

#lsblk -f (this would show up block devices along with file system)
List Swap Device#swapon -s
#cat /proc/swaps
Check RAM(Random Access Memory)“free” {#free -m Or #free -g = display in Megabytes/Gigabytes format}
#cat /proc/meminfo
Check Modules Loaded#lsmod
Check Mounted Devices“mount” (#mount)
#cat /proc/mounts
#cat /etc/mtab
“df” (#df -h)
Check File System Usage#df -Th
Check Hostname#hostname

#hostname -f {this would show-up the Fully Qualified Domain Name}
Check System Uptime#uptime
Check SELinux Status
(Security Enhanced Linux)
#cat /etc/sysconfig/selinux
Find The IP Address“ifconfig” {#ifconfig -a}
Current User Logged-in#who -s
Check Current Date & Time#date
Switch User“su”
Shutdown Command#shutdown -h now
#init 0
Reboot Command#shutdown -r now
#init 6
Check Current Runlevel#runlevel
#who -r
Switch from Runlevel 3 (text mode) to Runlevel 5 (GUI Mode)#init 5
Switch from Runlevel 5 to Runlevel 3#init 3
Create a Partition on a Hard DriveUsing “fdisk” or “parted” command
Create ext4 File System#mkfs.ext4 <DeviceName>

#mke2fs -t ext4 <DeviceName>
Create ext3 File System#mkfs.ext3 <DeviceName>

#mke2fs -t ext3 <DeviceName>
Check File System for Errors {recommended to un-mount file system before running this command}#e2fsck -f -y <DeviceName> {-f =force, -y =set automatic answer Yes}
Check if Network Interface is Up {I’ve taken first network interface “eth0” into reference here}#ping localhost

#ethtool eth0 {check for “Link Detected”}

#ifconfig eth0 {check for “UP” in the fourth line}
Check Running Processes#ps aux

#ps -ef

Search for a file#locate <FileName>

#find <PathWhereToSearch> -name <FileName> -type f

Ex:#find / -name hello.txt -type f {this would search for the file “hello.txt” under the / (root) directory}
Search for a word in a file“grep”

#grep -i <Word> <FileName> {this would search for the “word” within “FileName” regardless of case}
View top 10 lines in a file“head”

#head <FileName>
View bottom 10 lines in a file“tail”
#tail <FileName>
Check All the Daemons/Services Running#service --status-all
List Services Started in Run-level 5#chkconfig --list |grep “5:on”

#chkconfig --list {This would list out all the services/daemons running and their status in each run level}

#chkconfig --list |grep “3:on” {this would list out all the services which are running on run-level 3}

NOTE: These are a few basic commands commonly used, there would be a many alternatives available, to get complete syntax help check out the “man” pages of a command.


Anonymous said...

this is good.

bhargavi laxmi said...

Brilliant post, Thanks for such detailed explanation. Looking forward for more of your work.
Linux Online Training
Linux Training in Hyderabad
Linux Online Training in Hyderabad
Linux Online Training in India
Linux Online Training Institutes in Hyderabad
Best Institutes for Linux in Hyderabad
Linux Training Institutes in Hyderabad
Linux Training Institutes in India

Anu. SSDN said...

I really enjoyed while reading your article, the information you have mentioned in this post was damn good. Keep sharing your blog with updated and useful information.
We also provide linux corporate training in delhi