Date post: | 07-Apr-2018 |
Category: |
Documents |
Upload: | kidding-rider |
View: | 234 times |
Download: | 0 times |
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 1/18
Command Summary Use
Use cd to change
directories
Type cd followed by the name of a directory to access that
directory.
Keep in mind that you are always in a directory and allowed access
to any directories hierarchically above or below. You may also
benefit from reviewing my directory commands page.Ex:cd games
If the directory games is not located hierarchically below the
current directory, then the complete path must be written out. Ex:cd /usr/games
To move up one directory, use the shortcut command. Ex:cd ..
Use clear to clear thecommand prompt
Type clear to clean up your command prompt window.
This is especially helpful when you are typing lots of commandsand need a clean window to help you focus.Ex:
clearThis is also useful when you are getting ready to type a rather long
command and do not wish to become confused by other details on
the screen. Brought to you by Reallylinux.com
Use date to set yourserver's date and time
Type date followed by the two digit month, the two digit date, the
two digit time, and two digit minutes. The syntax is easy enoughand resembles this: MMDDhhmm
This command is helpful but must be used when superuser or
logged in as root.
You can get more help with administrator commands by clickingthis link . Note that if you don't use root, you will get an "Operation
not permitted" reply.As root user you can use thecommand such as:date 11081300
The above command will set the server date and time to the 11th
month (November), the 8th day, at 1:00pm.
Use df to check disk space
Typing df provides a very quick check of your file system disk
space.
Type df -h to get a more easily readable version of the output.Notice that this command will include all applicable storage such
as your hard disk/s (hda, hdb etc.) and your server SWAP file(shm). To list disk space including filesystem type:df -h -T
Use finger to see who's
on the system
Typing finger allows you to see who else is on the system or get
detailed information about a person who has access to the system.
Type finger followed by the name of a user's account to getinformation about that user. Or, type finger and press enter to see
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 2/18
who's on the system and what they are doing. Ex:finger johndoe
Use logout to quit usingthe system
Yep, you guessed it, typing logout will log your account out of the
system.
Type logout at the prompt to disconnect from your Linux machineor to logout a particular user session from the system. Keep in mind
that although rudimentary, leaving your critical account logged on
may be a security concern. We always recommend promptly usinglogout when you are finished using your root account! Ex:logout
Use ls to list files and
directories
Type ls to see a list of the files and directories located in the
current directory. If you’re in the directory named games and youtype ls, a list will appear that contains files in the games directory
and sub-directories in the games directory. Examples:ls Maills /usr/bin
Type ls -alt to see a list of all files (including .rc files) and alldirectories located in the current directory. The listing will include
detailed, often useful information. Examples:ls -altls -alt /usr/bin
If the screen flies by and you miss seeing a number of files, tryusing the |more at the end like:ls -alt |more
* In Bash (Linux shell) often the abbreviated command L is
available. To get a verbose listing of files and directories you couldtherefore simply type: l
Use man to pull upinformation about a Linux
command
Type man followed by a command to get detailed information
about how to use the command. Ex:man ls
Type man -k followed by a word to list all of the commands and
descriptions that contain the word you specified. Ex:man -k finger
Use more to read thecontents of a file
Type more followed by the name of a text file to read the file’s
contents. Why do we exmphasize using this on a "text" file?Because most other types of files will look like garbage! Ex:more testfile.txt
Use nano to start a texteditor
Typing nano will start a basic text editor on most Linux systems.Type nano followed by the filename you wish to edit. This basic
editor is quick and easy to use for beginners. However, it is very
important that you also learn about other text editors available on
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 3/18
Linux and UNIX systems. Click on this link to learn about otherslike emacs, vi, and pico. Ex:nano /etc/security/access.conf
Use passwd to change
your current password
Type passwd and press enter. You'll see the message Changing
password for yourname.At the Old password: prompt, type in your old password .
Then, at the Enter new password: prompt, type in your new
password .The system double checks your new password. Beside the Verify:
prompt, type the new password and press again.
Create a secure password that combines parts of words and
numbers. For instance, your dog's name may be Rufus. He may
have been born in 1980. Create a password that uses parts of boththe name and date of birth, such as 80rufuS. Note the use of at least
one capital letter. This is a fairly secure password and easy toremember.
Use pwd to list the name of
your current directory
Type pwd and hit enter. You'll see the full name of the directory
you are currently in. This is your directory path and is very handy.This is especially handy when you forget which directory you’ve
changed to and are trying to run other commands.
Manipulating Files - Linux Commands
Command Summary Use
chmod The chmod command allows you to alter accessrights to files and directories. All filesand directories have security permissionsthat grant the user particular groups’ or allother users’ access.
To view your files' settings, at the shellprompt type: ls -alt
You should see some files with the followingin front of them (an example follows):
total 4drwxrwsr-x 7 reallyli reallyli 1024 Apr 6 14:30 .drwxr-s--x 22 reallyli reallyli 1024 Mar 30 18:20 ..d-wx-wx-wx 3 reallyli reallyli 1024 Apr 6 14:30contentdrwxr-xr-x 2 reallyli reallyli 1024 Mar 25 20:43 files
What do the letters mean in front of thefiles/directories mean?r indicates that it is readable (someone can
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 4/18
view the file’s contents)w indicates that it is writable (someone canedit the file’s contents)x indicates that it is executable (someonecan run the file, if executable)- indicates that no permission to manipulate
has been assigned
When listing your files, the first characterlets you know whether you’re looking at afile or a directory. It’s not part of thesecurity settings. The next three charactersindicate Your access restrictions. The nextthree indicate your group's permissions, andfinally other users' permissions.
Use chmod followed by the permission you
are changing. In very simple form this would
be:chmod 755 filename The example above will grant you full rights,group rights to execute and read, and allothers access to execute the file.
# Permission
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none
Still confused? Use the table above to definethe settings for the three "users." In thecommand, the first number refers to your permissions, the second refers to group, andthe third refers to general users.
Typing the command: chmod 751 filename
gives you full access, the group read andexecute, and all others execute onlypermission.
cp Type cp followed by the name of an existing
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 5/18
file and the name of the new file.
Ex:cp newfile newerfileTo copy a file to a different directory(without changing th
e file’s name), specify the directory insteadof the newfilename. Ex:cp newfile testdirTo copy a file to a different directory andcreate a new file name, you need to specify adirectory/a new file name. Ex:cp newfile testdir/newerfilecp newfile ../newerfileThe .. represents one directory up in thehierarchy.
file Type file followed by the name of an existingfile in the directory.
Ex:file emergency3_demo.exe
OUTPUT: MS-DOS executable (EXE)
This command allows you to figure out whatthe file type is and how to use it. Forinstance the command will tell you whether itis an executable, a compressed file and whichtype, or something unusual.
This command is simplistic, but often canallow you to determine why a file does notrespond the way you expect.
mv Type mv followed by the current name of afile and the new name of the file.
Ex:mv oldfile newfile
Type mv followed by the name of a file andthe new directory where you'd like to placethe file. Ex:mv newfile testdirThis moves the file named newfile to anexisting directory named testdir. Be certainyou’re specifying a directoryname or the mv command alters the name of thefile instead of moving it.
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 6/18
rm Type rm followed by the name of a file toremove the file.
Ex:rm newfileUse the wildcard character to remove several
files at once. Ex:rm n*This command removes all files beginning withn.Type rm -i followed by a filename if you’dlike to be prompted before the file isactually removed. Ex:rm -i newfilerm -i n*By using this option, you have a chance toverify the removal of each file. The -ioption is very handy when removinga number of files using the wildcard
character *.
This list only has items related to files, but this link will take you to the page related tocommands for directories.
Directory Related Commands
Command Summary Use
cd Use cd to change directories. Type cdfollowed by the name of a directory to access
that directory. Keep in mind that you arealways in a directory and can navigate todirectories hierarchically above or below.Ex:cd gamesIf the directory games is not locatedhierarchically below the current directory,then the complete path must bewritten out. Ex:cd /usr/gamesTo move up one directory, use the shortcutcommand. Ex:cd ..
Use cp -r to copy a directory and all of itscontentsType cp -r followed by the name of anexisting directory and the name of the newdirectory. Ex:cp -r testing newdirYou must include the -r or you’ll see thefollowing message:
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 7/18
cp: testing is a directory and -r notspecified.This command saves you time if you need tomake a mirror image of a directory packedwith files.
mkdir Use mkdir to make/create a brand newdirectoryType mkdir followed by the name of adirectory. Ex:mkdir testdir
mv Use mv to change the name of a directoryType mv followed by the current name of adirectory and the new name of the directory.Ex:mv testdir newnamedir
pwd
Trying to find out where on your Linux serveryou currently are located? The pwd (printworking directory) command will show you thefull path to the directory you are currentlyin. This is very handy to use, especiallywhen performing some of the other commands onthis page!
rmdir Use rmdir to remove an existing directory(assuming you have permissions set to allowthis).
Type rmdir followed by a directory's name toremove it. Ex:rmdir testdir
You CAN'T remove a directory that containsfiles with this command.
Instead to delete non-empty directories inLinux, use the more useful command:rm -r
This command removes directories and fileswithin the directories. Read more detailsabout this in Commands for Beginning Admins
The rmdir command is used mostly to removeempty directories. If you have a desire touse this command then you'll need to deleteor move the files before attempting to removea full directory. For more help please readthe mv command and also File RelatedCommands.
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 8/18
Command Summary Use
du
The du command prints a summary of the amountof information you have stored in yourdirectories on the mounted disks.syntax: du [options] pathex: du -a /News
Options:-s print the sum of bytes in your directories-a print a line for each file in yourdirectory
grep
The grep command searches text files for aparticular word or string of words. Very
helpful when trying to find that needle in ahaystack, like a particular line in a largelog file.syntax: grep textstring filename(s)ex: grep century history.text.doc
Head
Tail
head: prints the beginning of a text filetail: prints the end of a text fileThese commands allow you to view parts of atext file.
tail -n 5 textfile.txthead -n 5 textfile.txt The examples above will print the last 5lines of the file textfile.txt and then thefirst 5 lines.
locate
Trying to find out where on your Linux servera particular file resides? Having a realnasty time doing it? If you have the Bashshell you can try using the locate command toidentify where it is on your mounted drives.Type: locate filename and press enter.Replace filename with the name of the fileyou are looking for. This is a real timesaving command as you start navigating yourLinux server!If locate does not work for you try using
which.
Nice
Nohup
Nice: runs programs/commands at a lowersystem priority Nohup: runs nice programs even when you’relogged off the systemBy using the two commands simultaneously,your large processes can continue to run,even when you have logged off the system andare relaxing.
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 9/18
Ex: nice nohup c program.c .This command will allow the c compiler tocompile program.c even when you have loggedoff the system.
ps
related to "stopped jobs"
The ps command displays all of the existingprocesses. This command is also directly
linked to issues with stopped processes (alsoknown as "stopped jobs").Occasionally, you may see the message Thereare Stopped Jobs. If you log off the system without properlystopping your jobs, some jobs/processes mayremain in memory tying up the system anddrawing unnecessary processing bandwidth.
Type ps and hit enter. This will list all ofyour current processes running, or stopped.
PID TT STAT TIME COMMAND
23036 pl S 0:00 -csh23070 pl R 0:00 vi
The number under PID is the processidentification number. To kill a process thatis stopped , type: kill pid . Replace pid withthe exact number of the process.Ex: While in Vi, you accidentally press thewrong keys. Vi's operation is stopped and youare kicked back to the prompt. To kill thestopped Vi command, you may type: kill 23070.
stty
The stty command allows you to view a listingof your current terminal options. By usingthis command, you can also remap keyboardkeys, tailoring to your needs.Ex: stty and hit enter. This lists yourterminal settings.Ex: stty erase\^h . This remaps your erasekey (backspace) to the Ctrl and h keys. Fromnow on, holding down Ctrl and pressing h willcause a backspace. So you're scratching yourhead asking why is this handy? You'll see howhelpful it can be to use.
talk
In order to contact someone who is on thesystem, at the prompt you type: talk
accountname . Replace accountname with thefull account name of the person. If you don’twant anyone to disturb you using the talkcommand, at the prompttype: mesg n. This prevents others from usingtalk to reach you.
tar
also related to gzip
You're bound to come across files that are g-zipped and tarred. Okay, now what? These aremethods of compressing and storing
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 10/18
directories and files in a single "file."Most new Linux programs come off the web assomething like coolnew-game.4-4-01.gz. Thisfile is likely a tar file that has then beengzipped for compression. The way to handlethese files is simple, but requires that you
put the file into an appropriate directory.In other words, don't place the file in yourroot or /bin unless it belongs there.
Now you can do a one fell swoop un-gzip itand untar it into its original form (usuallymultiple files in many sub directories) by
typing: tar -xvzf *.gz
This will programmatically un-gzip and thenuntar all files in the current directory intotheir full original form including sub-directories etc. Please be careful where andhow you run this!
w
This command allows you to list all users’and their processes who are currently loggedin to the Linux server, or a particularuser’s processes. Type: w to view all users’processes. Type: w jsmith to view jsmith’sprocesses. We use this all the time from asystem admin standpoint. Please also see morecommands to get user information on thispage. You need to know who logs on to yoursystem! Okay, so you have a stand alone Linuxbox and no one else uses it? Try this command
just to be sure. ;)
!!
Don’t waste time and energy retyping commandsat the prompt. Instead, use the ! option. Toautomatically re-display the last command youtyped at the prompt, type: !! and pressenter. Press again to invoke the command. Youcan also automatically re-display a commandyou typed earlier by using the ! and thefirst few letters of the command.Ex: At the Linux prompt you had typed thecommand clear , followed by the command pico,followed by the command ftp. In order to re-display the clear command you type: !cl andpress enter. In order to re-display the lastcommand you typed, simply type: !! . Try itout. You’ll find this a time saver whendealing with long commands. Especiallycommands like tar!
Basic Linux Commands
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 11/18
Command Example Description
cat Sends file contents to standard output. This is a way to
list the contents of short files to the screen. It works wellwith piping.
cat .bashrc Sends the contents of the ".bashrc" file to the screen.
cd Change directory
cd /home Change the current working directory to /home. The '/'
indicates relative to root, and no matter what directoryyou are in when you execute this command, the directory
will be changed to "/home".
cd httpd Change the current working directory to httpd, relative tothe current location which is "/home". The full path of
the new working directory is "/home/httpd".
cd .. Move to the parent directory of the current directory.
This command will make the current working directory
"/home.
cd ~ Move to the user's home directory which is
"/home/username". The '~' indicates the users home
directory.
cp Copy files
cp myfile yourfile Copy the files "myfile" to the file "yourfile" in the
current working directory. This command will create the
file "yourfile" if it doesn't exist. It will normallyoverwrite it without warning if it exists.
cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists, you will
be prompted before it is overwritten.cp -i /data/myfile . Copy the file "/data/myfile" to the current working
directory and name it "myfile". Prompt before
overwriting the file.
cp -dpr srcdir destdir Copy all files from the directory "srcdir" to the directory"destdir" preserving links (-p option), file attributes (-p
option), and copy recursively (-r option). With these
options, a directory and all it contents can be copied to
another directory.
dd dd if=/dev/hdb1
of=/backup/
Disk duplicate. The man page says this command is to
"Convert and copy a file", but although used by more
advanced users, it can be a very handy command. The"if" means input file, "of" means output file.
df Show the amount of disk space used on each mounted
filesystem.
less less textfile
Similar to the more command, but the user can page upand down through the file. The example displays the
contents of textfile.
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 12/18
ln Creates a symbolic link to a file.
ln -s test symlink Creates a symbolic link named symlink that points to thefile test Typing "ls -i test symlink" will show the two
files are different with different inodes. Typing "ls -l test
symlink" will show that symlink points to the file test.
locate A fast database driven file locator.
slocate -u 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 mostsystems.
locate whereis Lists all files whose names contain the string "whereis".
logout Logs the current user off the system.
ls List files
ls List files in the current working directory except those
starting with . and only show the file name.
ls -al List all files in the current working directory in long
listing format showing permissions, ownership, size, andtime and date stamp
more Allows file contents or piped output to be sent to the
screen one page at a time.
more /etc/profile Lists the contents of the "/etc/profile" file to the screenone page at a time.
ls -al |more Performs a directory listing of all files and pipes the
output of the listing through more. If the directory listing
is longer than a page, it will be listed one page at a time.
mv Move or rename files
mv -i myfile yourfile Move the file from "myfile" to "yourfile". This
effectively changes the name of "myfile" to "yourfile".
mv -i /data/myfile . Move the file from "myfile" from the directory "/data" tothe current working directory.
pwd Show the name of the current working directory
more /etc/profile Lists the contents of the "/etc/profile" file to the screen
one page at a time.
shutdown Shuts the system down.
shutdown -h now Shuts the system down to halt immediately.
shutdown -r now Shuts the system down immediately and the system
reboots.
whereis Show where the binary, source and manual page files arefor a command
whereis ls Locates binaries and manual pages for the ls command.
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 13/18
Editors: emacs, vi, pico, jed, vim
Viewing, copying, moving and deleting files
ls Display the contents of the current directory
ls -a Display also hidden files and hidden directories
cp filename /path/dir_name Copy filename into directory /path/dir_name
cp -r dir_name /path/dir_name2 Copy the entire dir_name into /path/dir_name2
cp filename1 filename2
/path/dir_nameCopy filename1 and filename2 into /path/dir_name
rm name Remove a file or directory called name
rm -r nameRemove an entire directory as well as its included files and
subdirectories
mv filename /path/dir_name Move filename into /path/dir_name
mv filename1 filename2 Rename filename1 to filename2
cat filename Display filenames contents
more filename Display filename in pages. Use spacebar to view next page
head filename Display filenames first 10 lines
head -15 filename Display filenames first 15 lines
tail filename Display filenames last 10 lines
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 14/18
tail -15 filename Display filenames last 15 lines
pwd Display current directory
cd /path/dir_name Change to directory /path/dir_name
cd .. Go 1 directory up
mkdir dir_name Create directory dir_name
rmdir dir_name Delete directory dir_name
Finding files and text within files
updatedbUpdate (create first time used) a database of all files under the root directory
/
locate filename Find file filename searching in the database
find / -name filename Starting from the root directory search for the file called filename
find / -name *filename Same 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_name Search $path for application app_name
whereis application_name Search $path, man pages and source files for application_name
Archived files
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 15/18
Decompress
tar -xzf filename.tgz Decompress tzg file
tar -xzf filename.tar.gz Decompress tar.gz file
tar -xjf filename.tar.bz2 Decompress tar.bz2 file
Compress
tar -czf filename.tar /path/dir_name Compress directory /path/dir_name to filename.tar
gzip -c filename > filename.gz Compress /path/dir_name to filename.tar.gz
bzip2 -c filename > filename.bz2 Compress /path/dir_name to filename.tar.bz2
Using rpm files
rpm -hiv package.rpm Install rpm called package.rpm
rpm -hiv --force package.rpm Install rpm called package.rpm by force
rpm -hUv package.rpm Upgrade rpm called package.rpm
rpm -e package.rpm Delete rpm called package.rpm
rpm -qpil package.rpm List files in not-installed rpm called package.rpm
rpm -ql package.rpm List files in installed rpm called package.rpm
rpm -q str List installed rpms containing the string str
rpm -qf /path/application_name Display the rpm that contains application application_name
Starting and Stoping
startx Start the X system
shutdown -h now Shutdown the system now and do not reboot
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 16/18
halt Same as above
shutdown -r now Reboot
reboot Same as above
shutdown -r +10 Reboot in 10 minutes
Mounting filesystems
mount -t vfat /dev/sd(a)(1)
/mnt/c_drive
Mount 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/hda1 Unmout the above
User administration
users Display users currently logged in
adduser username Create a new user called username
passwd username Define password for user called username
who List logged-in users
whoami Display current user
finger username Displays info about user username
su Log in as root from current login
su - Log in as root from current login and take root's path
exit Exit from console login (ie, logout).
Processes
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 17/18
command Execute command in the foreground
command & Execute command in the background
ctrl+z Suspend a program
ctrl+c Interrupt a program
ps List all processes
kill -9 pid Kill process with id pid
top Monitor processes in real time
Networking
hostname List the system's hostname
ifconfig Set/Display network information
host ip Resolves ip's hostname
ping ip/hostname Check if ip/hostname is reachable
traceroute ip/hostname Find network path to ip/hostname
System Information
uname -a General system information
fdisk -l List partition tables
cp filename /path/dir_name Copy filename into directory /path/dir_name
df -T -h List filesystem disk space usage
lspci List PCI devices
lsusb List USB devices
free -m Display RAM+Swap usage
8/6/2019 Linux Command 1
http://slidepdf.com/reader/full/linux-command-1 18/18
Program Compile
gcc -o output file.c Compile a C program
./output Run a C program you have compiled
g++ -o output file.cpp Compile a C++ program
./output Run a C++ program you have compiled
./configure && make && su -c 'make install' Configure, compile and install a program with Makefile