Sei sulla pagina 1di 18

Command

Use cd to change directories

Summary Use 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 .. Type clear to clean up your command prompt window. This is especially helpful when you are typing lots of commands and need a clean window to help you focus.Ex: clear This 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 Type date followed by the two digit month, the two digit date, the two digit time, and two digit minutes. The syntax is easy enough and resembles this: MMDDhhmm This command is helpful but must be used when superuser or logged in as root.

Use clear to clear the command prompt

Use date to set your server's date and time

You can get more help with administrator commands by clicking this link. Note that if you don't use root, you will get an "Operation not permitted" reply.As root user you can use the command 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.

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. Use df to check disk space 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 Typing finger allows you to see who else is on the system or get Use finger to see who's detailed information about a person who has access to the system. Type finger followed by the name of a user's account to get on the system information about that user. Or, type finger and press enter to see

who's on the system and what they are doing. Ex: finger johndoe Yep, you guessed it, typing logout will log your account out of the system. Type logout at the prompt to disconnect from your Linux machine Use logout to quit using or to logout a particular user session from the system. Keep in mind that although rudimentary, leaving your critical account logged on the system may be a security concern. We always recommend promptly using logout when you are finished using your root account! Ex: logout Type ls to see a list of the files and directories located in the current directory. If youre in the directory named games and you type ls, a list will appear that contains files in the games directory and sub-directories in the games directory. Examples: ls Mail ls /usr/bin Type ls -alt to see a list of all files (including .rc files) and all directories located in the current directory. The listing will include detailed, often useful information. Examples: ls -alt ls -alt /usr/bin If the screen flies by and you miss seeing a number of files, try using 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 could therefore simply type: l 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 Type more followed by the name of a text file to read the files 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 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

Use ls to list files and directories

Use man to pull up information about a Linux command

Use more to read the contents of a file

Use nano to start a text editor

Linux and UNIX systems. Click on this link to learn about others like emacs, vi, and pico. Ex: nano /etc/security/access.conf 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 both the 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 to remember.

Use passwd to change your current password

Type pwd and hit enter. You'll see the full name of the directory Use pwd to list the name of you are currently in. This is your directory path and is very handy. This is especially handy when you forget which directory youve your current directory changed to and are trying to run other commands.

Manipulating Files - Linux Commands


Command
chmod

Summary Use
The chmod command allows you to alter access rights to files and directories. All files and directories have security permissions that grant the user particular groups or all other users access. To view your files' settings, at the shell prompt type: ls -alt You should see some files with the following in front of them (an example follows):
total 4 drwxrwsr-x drwxr-s--x d-wx-wx-wx content drwxr-xr-x 7 reallyli reallyli 1024 Apr 6 14:30 . 22 reallyli reallyli 1024 Mar 30 18:20 .. 3 reallyli reallyli 1024 Apr 6 14:30 2 reallyli reallyli 1024 Mar 25 20:43 files

What do the letters mean in front of the files/directories mean? r indicates that it is readable (someone can

view the files contents) w indicates that it is writable (someone can edit the files contents) x indicates that it is executable (someone can run the file, if executable) - indicates that no permission to manipulate has been assigned When listing your files, the first character lets you know whether youre looking at a file or a directory. Its not part of the security settings. The next three characters indicate Your access restrictions. The next three indicate your group's permissions, and finally 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 all others 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 define the settings for the three "users." In the command, the first number refers to your permissions, the second refers to group, and the third refers to general users. Typing the command: chmod 751 filename gives you full access, the group read and execute, and all others execute only permission.

cp

Type cp followed by the name of an existing

file and the name of the new file. Ex: cp newfile newerfile To copy a file to a different directory (without changing th e files name), specify the directory instead of the new filename. Ex: cp newfile testdir To copy a file to a different directory and create a new file name, you need to specify a directory/a new file name. Ex: cp newfile testdir/newerfile cp newfile ../newerfile The .. represents one directory up in the hierarchy. file Type file followed by the name of an existing file in the directory. Ex: file emergency3_demo.exe OUTPUT: MS-DOS executable (EXE) This command allows you to figure out what the file type is and how to use it. For instance the command will tell you whether it is an executable, a compressed file and which type, or something unusual. This command is simplistic, but often can allow you to determine why a file does not respond the way you expect. mv Type mv followed by the current name of a file and the new name of the file. Ex: mv oldfile newfile Type mv followed by the name of a file and the new directory where you'd like to place the file. Ex: mv newfile testdir This moves the file named newfile to an existing directory named testdir. Be certain youre specifying a directory name or the mv command alters the name of the file instead of moving it.

rm

Type rm followed by the name of a file to remove the file. Ex: rm newfile Use the wildcard character to remove several files at once. Ex: rm n* This command removes all files beginning with n. Type rm -i followed by a filename if youd like to be prompted before the file is actually removed. Ex: rm -i newfile rm -i n* By using this option, you have a chance to verify the removal of each file. The -i option is very handy when removing a 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 to commands for directories.

Directory Related Commands


Command cd 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 can navigate to directories hierarchically above or below. 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 cp -r to copy a directory and all of its contents Type cp -r followed by the name of an existing directory and the name of the new directory. Ex: cp -r testing newdir You must include the -r or youll see the following message:

cp: testing is a directory and -r not specified. This command saves you time if you need to make a mirror image of a directory packed with files.

mkdir

Use mkdir to make/create a brand new directory Type mkdir followed by the name of a directory. Ex: mkdir testdir Use mv to change the name of a directory Type mv followed by the current name of a directory and the new name of the directory. Ex: mv testdir newnamedir Trying to find out where on your Linux server you currently are located? The pwd (print working directory) command will show you the full path to the directory you are currently in. This is very handy to use, especially when performing some of the other commands on this page! Use rmdir to remove an existing directory (assuming you have permissions set to allow this). Type rmdir followed by a directory's name to remove it. Ex: rmdir testdir You CAN'T remove a directory that contains files with this command. Instead to delete non-empty directories in Linux, use the more useful command:rm -r This command removes directories and files within the directories. Read more details about this in Commands for Beginning Admins The rmdir command is used mostly to remove empty directories. If you have a desire to use this command then you'll need to delete or move the files before attempting to remove a full directory. For more help please read the mv command and also File Related Commands.

mv

pwd

rmdir

Command

du

Summary Use The du command prints a summary of the amount of information you have stored in your directories on the mounted disks. syntax: du [options] path ex: du -a /News Options: -s print the sum of bytes in your directories -a print a line for each file in your directory The grep command searches text files for a particular word or string of words. Very helpful when trying to find that needle in a haystack, like a particular line in a large log file. syntax: grep textstring filename(s) ex: grep century history.text.doc head: prints the beginning of a text file tail: prints the end of a text file These commands allow you to view parts of a text file. tail -n 5 textfile.txt head -n 5 textfile.txt The examples above will print the last 5 lines of the file textfile.txt and then the first 5 lines. Trying to find out where on your Linux server a particular file resides? Having a real nasty time doing it? If you have the Bash shell you can try using the locate command to identify where it is on your mounted drives. Type: locate filename and press enter. Replace filename with the name of the file you are looking for. This is a real time saving command as you start navigating your Linux server! If locate does not work for you try using which. Nice: runs programs/commands at a lower system priority Nohup: runs nice programs even when youre logged off the system By using the two commands simultaneously, your large processes can continue to run, even when you have logged off the system and are relaxing.

grep

Head Tail

locate

Nice Nohup

Ex: nice nohup c program.c . This command will allow the c compiler to compile program.c even when you have logged off the system. The ps command displays all of the existing processes. This command is also directly linked to issues with stopped processes (also known as "stopped jobs"). Occasionally, you may see the message There are Stopped Jobs. If you log off the system without properly stopping your jobs, some jobs/processes may remain in memory tying up the system and drawing unnecessary processing bandwidth. ps
related to "stopped jobs"

Type ps and hit enter. This will list all of your current processes running, or stopped.
PID TT STAT TIME COMMAND 23036 pl S 0:00 -csh 23070 pl R 0:00 vi

stty

talk

tar
also related to gzip

The number under PID is the process identification number. To kill a process that is stopped, type: kill pid. Replace pid with the exact number of the process. Ex: While in Vi, you accidentally press the wrong keys. Vi's operation is stopped and you are kicked back to the prompt. To kill the stopped Vi command, you may type: kill 23070. The stty command allows you to view a listing of your current terminal options. By using this command, you can also remap keyboard keys, tailoring to your needs. Ex: stty and hit enter. This lists your terminal settings. Ex: stty erase\^h . This remaps your erase key (backspace) to the Ctrl and h keys. From now on, holding down Ctrl and pressing h will cause a backspace. So you're scratching your head asking why is this handy? You'll see how helpful it can be to use. In order to contact someone who is on the system, at the prompt you type: talk accountname . Replace accountname with the full account name of the person. If you dont want anyone to disturb you using the talk command, at the prompt type: mesg n. This prevents others from using talk to reach you. You're bound to come across files that are gzipped and tarred. Okay, now what? These are methods of compressing and storing

directories and files in a single "file." Most new Linux programs come off the web as something like coolnew-game.4-4-01.gz. This file is likely a tar file that has then been gzipped for compression. The way to handle these files is simple, but requires that you put the file into an appropriate directory. In other words, don't place the file in your root or /bin unless it belongs there. Now you can do a one fell swoop un-gzip it and untar it into its original form (usually multiple files in many sub directories) by typing: tar -xvzf *.gz This will programmatically un-gzip and then untar all files in the current directory into their full original form including subdirectories etc. Please be careful where and how you run this! This command allows you to list all users and their processes who are currently logged in to the Linux server, or a particular users processes. Type: w to view all users processes. Type: w jsmith to view jsmiths processes. We use this all the time from a system admin standpoint. Please also see more commands to get user information on this page. You need to know who logs on to your system! Okay, so you have a stand alone Linux box and no one else uses it? Try this command just to be sure. ;) Dont waste time and energy retyping commands at the prompt. Instead, use the ! option. To automatically re-display the last command you typed at the prompt, type: !! and press enter. Press again to invoke the command. You can also automatically re-display a command you typed earlier by using the ! and the first few letters of the command. Ex: At the Linux prompt you had typed the command clear, followed by the command pico, followed by the command ftp. In order to redisplay the clear command you type: !cl and press enter. In order to re-display the last command you typed, simply type: !! . Try it out. Youll find this a time saver when dealing with long commands. Especially commands like tar!

!!

Basic Linux Commands

Command cat

cd

cp

dd

df less

Description Sends 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 .bashrc Sends the contents of the ".bashrc" file to the screen. Change directory cd /home Change 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 httpd Change 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. 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 normally overwrite 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. Disk duplicate. The man page says this command is to dd if=/dev/hdb1 "Convert and copy a file", but although used by more of=/backup/ advanced users, it can be a very handy command. The "if" means input file, "of" means output file. Show the amount of disk space used on each mounted filesystem. Similar to the more command, but the user can page up less textfile and down through the file. The example displays the contents of textfile.

Example

Creates a symbolic link to a file. ln -s test symlink Creates 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. A fast database driven file locator. locate 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 most systems. locate whereis Lists all files whose names contain the string "whereis". Logs the current user off the system. logout List files ls 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, and time and date stamp Allows file contents or piped output to be sent to the more screen one page at a time. more /etc/profile Lists the contents of the "/etc/profile" file to the screen one 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. Move or rename files mv 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" to the current working directory. Show the name of the current working directory pwd more /etc/profile Lists the contents of the "/etc/profile" file to the screen one page at a time. Shuts the system down. shutdown shutdown -h now Shuts the system down to halt immediately. shutdown -r now Shuts the system down immediately and the system reboots. Show where the binary, source and manual page files are whereis for a command whereis ls Locates binaries and manual pages for the ls command. ln

Editors: emacs, vi, pico, jed, vim

Viewing, copying, moving and deleting files


ls ls -a Display the contents of the current directory Display also hidden files and hidden directories

cp filename /path/dir_name cp -r dir_name /path/dir_name2 cp filename1 filename2

Copy filename into directory /path/dir_name Copy the entire dir_name into /path/dir_name2

Copy filename1 and filename2 into /path/dir_name /path/dir_name

rm name

Remove a file or directory called name Remove an entire directory as well as its included files and

rm -r name subdirectories

mv filename /path/dir_name mv filename1 filename2

Move filename into /path/dir_name Rename filename1 to filename2

cat filename

Display filenames contents

more filename

Display filename in pages. Use spacebar to view next page

head filename head -15 filename

Display filenames first 10 lines Display filenames first 15 lines

tail filename

Display filenames last 10 lines

tail -15 filename

Display filenames last 15 lines

pwd

Display current directory

cd /path/dir_name cd ..

Change to directory /path/dir_name Go 1 directory up

mkdir dir_name rmdir dir_name

Create directory dir_name Delete directory dir_name

Finding files and text within files


Update (create first time used) a database of all files under the root directory updatedb / locate filename Find file filename searching in the database

find / -name filename find / -name *filename

Starting from the root directory search for the file called filename Same as above but search for file containing the string filename

grep string Starting from /path/dir_name search for all files containing string /path/dir_name

which application_name

Search $path for application app_name

whereis application_name Search $path, man pages and source files for application_name

Archived files

Decompress

tar -xzf filename.tgz tar -xzf filename.tar.gz tar -xjf filename.tar.bz2

Decompress tzg file Decompress tar.gz file Decompress tar.bz2 file

Compress

tar -czf filename.tar /path/dir_name gzip -c filename > filename.gz bzip2 -c filename > filename.bz2

Compress directory /path/dir_name to filename.tar Compress /path/dir_name to filename.tar.gz Compress /path/dir_name to filename.tar.bz2

Using rpm files


rpm -hiv package.rpm rpm -hiv --force package.rpm rpm -hUv package.rpm rpm -e package.rpm rpm -qpil package.rpm rpm -ql package.rpm rpm -q str rpm -qf /path/application_name Install rpm called package.rpm Install rpm called package.rpm by force Upgrade rpm called package.rpm Delete rpm called package.rpm List files in not-installed rpm called package.rpm List files in installed rpm called package.rpm List installed rpms containing the string str Display the rpm that contains application application_name

Starting and Stoping


startx shutdown -h now Start the X system Shutdown the system now and do not reboot

halt shutdown -r now reboot shutdown -r +10

Same as above Reboot Same as above Reboot in 10 minutes

Mounting filesystems
mount -t vfat /dev/sd(a)(1) /mnt/c_drive mount -t iso9660 /dev/cdrom Mount cdrom under /mnt/cdrom directory /mnt/cdrom Mount the first partition 1 of the first hard disk drive a which is in fat32 vfat dormat under /mnt/c_drive directory

umount /mnt/hda1

Unmout the above

User administration
users adduser username passwd username who whoami finger username su su exit Display users currently logged in Create a new user called username Define password for user called username List logged-in users Display current user Displays info about user username Log in as root from current login Log in as root from current login and take root's path Exit from console login (ie, logout).

Processes

command command & ctrl+z ctrl+c ps kill -9 pid top

Execute command in the foreground Execute command in the background Suspend a program Interrupt a program List all processes Kill process with id pid Monitor processes in real time

Networking
hostname ifconfig host ip ping ip/hostname traceroute ip/hostname List the system's hostname Set/Display network information Resolves ip's hostname Check if ip/hostname is reachable Find network path to ip/hostname

System Information
uname -a fdisk -l cp filename /path/dir_name df -T -h lspci lsusb free -m General system information List partition tables Copy filename into directory /path/dir_name List filesystem disk space usage List PCI devices List USB devices Display RAM+Swap usage

Program Compile
gcc -o output file.c ./output g++ -o output file.cpp ./output ./configure && make && su -c 'make install' Compile a C program Run a C program you have compiled Compile a C++ program Run a C++ program you have compiled Configure, compile and install a program with Makefile

Potrebbero piacerti anche