Linux Basic Commands

It is my hope that these beginner Linux commands will provide you with a good start to your Linux command line adventures.

TIP 1:
All of these beginner Linux commands should work from your command prompt (regardless which shell you’re using). Just in case some folks were not aware, you MUST press enter to invoke the command, and I try to cover the Linux commands I use most often such as cd, pwd, finger and man.TIP 2:
For this summary, please note that the EX: stands for example and is not part of the command. Commands are denoted in courier type font.

TIP 3:
If you need help understanding what the options are, or how to use a command, try adding this to the end of your command: –help

For example, for better understanding of the df command’s options, type:
df –help

So, you have installed your favorite linux distro and you are sitting in front of it. Probably you must have heard a lot of frightening things about its console. Here you will see some basic linux commands which will help you to get familiar with linux command line. So let’s continue with the linux commands cheatsheet.

But first a few words about a very basic command called man. man comes from manual and it works like this man command_name. With this command you can view information on how to use any command of your system. There is even a man page for man! Try typing man man and you ll get the point. So if want further information and details on a command listed below just type man command_name

Viewing, copying, moving and deleting files

lsDisplay the contents of the current directory
ls -aDisplay also hidden files and hidden directories
cp filename /path/dir_nameCopy filename into directory /path/dir_name
cp -r dir_name /path/dir_name2Copy the entire dir_name into /path/dir_name2
cp filename1 filename2 /path/dir_nameCopy filename1 and filename2 into /path/dir_name
rm nameRemove a file or directory called name
rm -r nameRemove an entire directory as well as its included files and subdirectories
mv filename /path/dir_nameMove filename into /path/dir_name
mv filename1 filename2Rename filename1 to filename2
cat filenameDisplay filenames contents
more filenameDisplay filename in pages. Use spacebar to view next page
head filenameDisplay filenames first 10 lines
head -15 filenameDisplay filenames first 15 lines
tail filenameDisplay filenames last 10 lines
tail -15 filenameDisplay filenames last 15 lines
pwdDisplay current directory
cd /path/dir_nameChange to directory /path/dir_name
cd ..Go 1 directory up
mkdir dir_nameCreate directory dir_name
rmdir dir_nameDelete directory dir_name

Finding files and text within files

updatedbUpdate (create first time used) a database of all files under the root directory /
locate filenameFind file filename searching in the database
find / -name filenameStarting from the root directory search for the file called filename
find / -name *filenameSame as above but search for file containing the string filename
grep string /path/dir_nameStarting from /path/dir_name search for all files containing string
which application_nameSearch $path for application app_name
whereis application_nameSearch $path, man pages and source files for application_name

Archived files

Decompress

tar -xzf filename.tgzDecompress tzg file
tar -xzf filename.tar.gzDecompress tar.gz file
tar -xjf filename.tar.bz2Decompress tar.bz2 file

Compress

tar -czf filename.tar /path/dir_nameCompress directory /path/dir_name to filename.tar
gzip -c filename > filename.gzCompress /path/dir_name to filename.tar.gz
bzip2 -c filename > filename.bz2Compress /path/dir_name to filename.tar.bz2

Using rpm files

rpm -hiv package.rpmInstall rpm called package.rpm
rpm -hiv –force package.rpmInstall rpm called package.rpm by force
rpm -hUv package.rpmUpgrade rpm called package.rpm
rpm -e package.rpmDelete rpm called package.rpm
rpm -qpil package.rpmList files in not-installed rpm called package.rpm
rpm -ql package.rpmList files in installed rpm called package.rpm
rpm -q strList installed rpms containing the string str
rpm -qf /path/application_nameDisplay the rpm that contains application application_name

Starting and Stoping

startxStart the X system
shutdown -h nowShutdown the system now and do not reboot
halt
Same as above
shutdown -r nowReboot
rebootSame as above
shutdown -r +10Reboot in 10 minutes

Mounting filesystems

mount -t vfat /dev/sd(a)(1) /mnt/c_driveMount the first partition 1 of the first hard disk drive a which is in fat32 vfat dormat under /mnt/c_drive directory
mount -t iso9660 /dev/cdrom /mnt/cdromMount cdrom under /mnt/cdrom directory
umount /mnt/hda1Unmout the above

User administration

usersDisplay users currently logged in
adduser usernameCreate a new user called username
passwd usernameDefine password for user called username
whoList logged-in users
whoamiDisplay current user
finger usernameDisplays info about user username
suLog in as root from current login
su –Log in as root from current login and take root’s path
exitExit from console login (ie, logout).

Processes

commandExecute command in the foreground
command &Execute command in the background
ctrl+zSuspend a program
ctrl+cInterrupt a program
psList all processes
kill -9 pidKill process with id pid
topMonitor processes in real time

Networking

hostnameList the system’s hostname
ifconfigSet/Display network information
host ipResolves ip’s hostname
ping ip/hostnameCheck if ip/hostname is reachable
traceroute ip/hostnameFind network path to ip/hostname

 System Information

uname -aGeneral system information
fdisk -lList partition tables
cp filename /path/dir_nameCopy filename into directory /path/dir_name
df -T -hList filesystem disk space usage
lspciList PCI devices
lsusbList USB devices
free -mDisplay RAM+Swap usage

 Program Compile

gcc -o output file.cCompile a C program
./outputRun a C program you have compiled
g++ -o output file.cppCompile a C++ program
./outputRun a C++ program you have compiled
./configure && make && su -c ‘make install’Configure, compile and install a program with Makefile

Leave a Reply

Your email address will not be published. Required fields are marked *

Close