Thursday, April 5, 2012

Basic Linux Commands

Linux Commands:-






catSends file contents to standard output. This is a way to list the contents of short files to the screen. It works well with piping.
cat .bashrcSends the contents of the ".bashrc" file to the screen.
cdChange directory
cd /homeChange the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home".
cd httpdChange the current working directory to httpd, relative to the 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.
cpCopy files
cp myfile yourfileCopy 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 normally overwrite it without warning if it exists.
cp -i myfile yourfileWith 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 destdirCopy 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.
dddd 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.
dfShow the amount of disk space used on each mounted filesystem.
lessless textfileSimilar to the more command, but the user can page up and down through the file. The example displays the contents of textfile.
lnCreates a symbolic link to a file.
ln -s test symlinkCreates a symbolic link named symlink that points to the file 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.
locateA fast database driven file locator.
slocate -uThis 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 most systems.
locate whereisLists all files whose names contain the string "whereis".
logoutLogs the current user off the system.
lsList files
lsList files in the current working directory except those starting with . and only show the file name.
ls -alList all files in the current working directory in long listing format showing permissions, ownership, size, and time and date stamp
moreAllows file contents or piped output to be sent to the screen one page at a time.
more /etc/profileLists the contents of the "/etc/profile" file to the screen one page at a time.
ls -al |morePerforms 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.
mvMove or rename files
mv -i myfile yourfileMove 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" to the current working directory.
pwdShow the name of the current working directory
more /etc/profileLists the contents of the "/etc/profile" file to the screen one page at a time.
shutdownShuts the system down.
shutdown -h nowShuts the system down to halt immediately.
shutdown -r nowShuts the system down immediately and the system reboots.
whereisShow where the binary, source and manual page files are for a command

whereis lsLocates binaries and manual pages for the ls command.








Editors: emacs, vi, pico, jed, vim



Linux Commands:-





A
  • alias - create names or abbreviations for commands
  • apropos - search the manual page names and descriptions
  • at  - queue, examine or delete jobs for later execution
B
  • bc  - An arbitrary precision calculator language
C
  • cal - displays a calendar
  • cat  - concatenate files and print on the standard output
  • cd - change directory
  • chgrp  - change group ownership
  • chmod  - change file access permissions
  • cksum  - checksum and count the bytes in a file
  • cp  - copy files and directories
  • csplit  - split a file into sections determined by context lines
D
  • date  - print or set the system date and time
  • dd  - convert and copy a file
  • du  - estimate file space usage
E
  • egrep  - print lines matching a pattern
  • export - set an environment variable
F

  • fgrep  - print lines matching a pattern
  • find - search for files in a directory hierarchy
  • fold  - wrap each input line to fit in specified width
G
  • grep  - print lines matching a pattern
H

  • head  - output the first part of files
I
J
  • join  - join lines of two files on a common field
K
L
  • logrotate  - rotates, compresses, and mails system logs
  • ls [man page] - list directory contents
M
  • mkdir  - make directories
  • mv  - move (rename) files
  • mount  - mount a file system
N
  • nl  - number lines of files
O
  • od  - dump files in octal and other formats
P
  • pwd  - print name of current/working directory
Q
R
  • rgrep  - print lines matching a pattern
  • rm  - remove files or directories
S
  • scp - secure copy (remote file copy program)
  • ssh - OpenSSH SSH client (remote login program)
T
  • tail - output the last part of files
  • tar  - The GNU version of the tar archiving utility
  • tee  - read from standard input and write to standard output and files
  • time  - run programs and summarize system resource usage
  • touch  - change file timestamps
V
  • vim - Vi IMproved, a programmers text editor
W
  • watch  - execute a program periodically, showing output fullscreen
  • wc  - print newline, word, and byte counts for each file
  • whoami  - print effective userid
X

  • yes  - output a string repeatedly until killed             



ls : list files/directories in a directory, comparable to dir in windows/dos.
ls -al : shows all files (including ones that start with a period), directories, and details attributes for each file.
cd : change directory
cd /usr/local/apache : go to /usr/local/apache/ directory
cd ~ : go to your home directory
cd – : go to the last directory you were in
cd .. : go up a directory
cat : print file contents to the screen
cat filename.txt : cat the contents of filename.txt to your screen
tail : like cat, but only reads the end of the file
tail /var/log/messages : see the last 20 (by default) lines of /var/log/messages
tail -f /var/log/messages : watch the file continuously, while it’s being updated
tail -200 /var/log/messages : print the last 200 lines of the file to the screen
more : like cat, but opens the file one screen at a time rather than all at once
more /etc/userdomains : browse through the userdomains file. hit to go to the next page, to quit
pico : friendly, easy to use editor. A clone of it is “nano”
pico /home/burst/public_html/index.html : edit the index page for the user’s website.
vi : another editor, tons of features, harder to use at first than pico
vi /home/burst/public_html/index.html : edit the index page for the user’s website.
grep : looks for patterns in files
grep root /etc/passwd : shows all matches of root in /etc/passwd
grep -v root /etc/passwd : shows all lines that do not match root
touch : create an empty file
touch /home/burst/public_html/404.html : create an empty file called 404.html in the directory /home/burst/public_html/
ln : create’s “links” between files and directories
ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf : Now you can edit /etc/httpd.conf rather than the original. changes
will affect the orginal, however you can delete the link and it will not delete the original.
rm : delete a file
rm filename.txt : deletes filename.txt, will more than likely ask if you really want to delete it.
rm -f filename.txt : deletes filename.txt, will not ask for confirmation before deleting.
rm -rf tmp/ : recursively deletes the directory tmp, and all files in it, including subdirectories. BE VERY CAREFULL WITH THIS COMMAND!!!
last : shows who logged in and when
last -20 : shows only the last 20 logins
last -20 -a : shows last 20 logins, with the hostname in the last field
w : shows who is currently logged in and where they are logged in from.
netstat : shows all current network connections.
netstat -an : shows all connections to the server, the source and destination ips and ports.
netstat -rn : shows routing table for all ips bound to the server.
file : attempts to guess what type of file a file is by looking at it’s content.
file * : prints out a list of all files/directories in a directory
du : shows disk usage.
du -sh : shows a summary, in human-readble form, of total disk space used in the current directory, including subdirectories.
du -sh * : same thing, but for each file and directory. helpful when finding large files taking up space.
wc : word count
wc -l filename.txt : tells how many lines are in filename.txt
cp : copy a file
cp filename filename.backup : copies filename to filename.backup
cp -a /home/burst/new_design/* /home/burst/public_html/ : copies all files, retaining permissions form one directory to another.
Putting commands together.
Often you will find you need to use different commands on the same line. Here are some examples.
Note that the | character is called a pipe, it takes date from one program and pipes it to another.
> means create a new file, overwriting any content already there.
>> means tp append data to a file, creating a newone if it doesn not already exist.
< send input from a file back into a command.
grep User /usr/local/apache/conf/httpd.conf |more
-- this will dump all lines that match User from the httpd.conf, then print the results to your screen one page at a time.
last -a > /root/lastlogins.tmp
— this will print all the current login history to a file called lastlogins.tmp in /root/
tail -10000 /var/log/exim_mainlog |grep domain\.com |more
— this will grab the last 10,000 lines from /var/log/exim_mainlog, find all occurances of domain.com (the period represents ‘anything’,
— comment it out with a \ so it will be interpretted literally), then send it to your screen page by page.
netstat -an |grep :80 |wc -l
— show how many active connections there are to apache (httpd runs on port 80)
mysqladmin processlist |wc -l
— show how many current open connections there are to mysql

No comments:

Post a Comment