Sunday, August 17, 2014

Linux Commands For A Beginner [Updated]

These are the most commonly used 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 to Linux, learn through Red Hat, click on 'Download Help & Learning' tab at the top for more details.



PurposeCommands
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}
OR
“vi <Filename> {allows editing new file}
Create a Directory“mkdir <DirectoryName>”
OR
“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”
Eg:# chmod o+w /testfile (sets write permission for this file for other users)

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

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

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

Eg:# 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 Red Hat Release Version# cat /etc/redhat-release

OR
# cat /etc/os-release
Check OS Architecture# arch

Or

# uname -m
List of PCI Devices Found# lspci
List of USB Devices Found# lsusb
List of Hard Drives Found“fdisk” {# fdisk -l}

OR
“parted” {# parted --list}
Get Processors Details“lscpu”
OR
# cat /proc/cpuinfo
OR
# dmidecode --type processor
Get Memory Modules Details# 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

OR
# cat /proc/swaps
Check RAM(Random Access Memory)“free” {# free -m Or # free -g = display in Megabytes/Gigabytes format}

OR
# cat /proc/meminfo
List Modules Loaded# lsmod
List Mounted Devices“mount” (# mount)
OR
# cat /proc/mounts
OR
# cat /etc/mtab
OR
“df” (# df -h)
Check File System Usage# df -Th
Check/Display Hostname
# hostname

# hostname -f {this would show-up the Fully Qualified Domain Name}
OR
# sysctl -n kernel.hostname

# cat /etc/hostname { RHEL7 & above }

Check System Uptime# uptime
Check SELinux Status
(Security Enhanced Linux)

# sestatus
OR
# cat /etc/sysconfig/selinux

# cat /etc/selinux/config { RHEL7 & above }

Find The IP Address
“ifconfig” {# ifconfig -a}
OR
# ip a

Find Current User Logged-in# who -s

OR
# whoami
Check Current Date & Time# date
Switch User“su”
Shutdown Command# shutdown -h now

OR
# poweroff

OR
# halt

OR
# init 0

# systemctl poweroff { RHEL7 & above }

Reboot Command# shutdown -r now

OR
# reboot

OR
# init 6

# systemctl reboot { RHEL7 & above }

Check Current Runlevel
# runlevel

OR
# who -r

# systemctl get-default { RHEL7 & above }

Switch from Runlevel 3 (text mode) to Runlevel 5 (GUI Mode)

{ In RHEL7, it is targets not runlevel }
# init 5

OR
# startx

# systemctl set-default graphical.target { RHEL7 & above }

# systemctl isolate graphical.target

Switch from Runlevel 5 to Runlevel 3
# init 3

# systemctl set-default multi-user.target { RHEL7 & above }

# systemctl isolate multi-user.target

Create a Partition on a Hard DriveUsing “fdisk” or “parted” command
Create ext4 File System# mkfs.ext4 <DeviceName>

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

OR
# 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

OR
# ethtool eth0 {check for “Link Detected”}

OR
# ifconfig eth0 {check for “UP” in the fourth line}

OR
# ip addr show eth0 { RHEL7 & above }

OR
# ip a s eth0

List out Running Processes# ps aux

OR
# ps -ef

OR
# top
Search for a file# locate <FileName>

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

Eg:# 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

# systemctl list-units --type service --all { RHEL7 & above }
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}

 # systemctl list-units --type service  { RHEL7 & above }


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.

2 comments:

Anonymous said...

this is good.

Anuradha Singhaniya 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