Quick links

Page links:


  Home
  Forums
  Linux Commands
  Desktop Wallpaper & Images
  Software
  HTML Sample Pages
  Z06 vs Top Fuel Dragster Fun Facts
  Links

 

  Common Linux Commands

I'm trying to compile a list of commands that are commonly used in Linux terminals to accomplish tasks. Hopefully, this will help new Linux users adapt and also serve as a reference for myself. Not all of the flags will be shown for each command.


Note: $ sign is not part of the command, it represents the prompt.

  • cp - Used to copy files. For example, to copy the file image1.jpg in the current directory to a subfolder called images, type cp -i image1.jpg images/. The -i flag prompts the user before any files that already exist are overwritten.
    Example: "$ cp -i textdocument.txt /home/username/folder1"

  • df - Prints disk space status to the screen. Use the -h flag to format the output in easier to understand units. Provide a specific drive as an argument to see only the stats about that drive.
    Example: "$ df -h /dev/hda1" - Prints total storage space, space used, and space available on hda1.
    Example: "$ df -h" - Prints total storage space, space used, and space available for all drives.

  • gpasswd - Used to add a user to a group. For example, to allow a user to be able to become root in a terminal, that user needs to be part of the wheel group (for Gentoo, other distro's may be different).
    Example: "$ gpasswd -a username groupname" - adds the user username to the group groupname.

  • kill - Used to stop or send signals to a process. Use kill -l to see a list of signals that can be sent to a process. To send that signal to a process you send the corresponding number to the process.
    Example: "$ kill -9 17534" - Sends signal SIGKILL to process with ID 17534. The "-9" option forces the process to quit, no matter what.

  • ls - Lists the contents of the directory you are currently working in. Add a target as an argument to list the contents of a directory other than the one you are currently working in. Add -F to put slashes after directory names.
    Example: "$ ls" - Lists the contents of the directory you are currently in.
    Example: "$ ls -F /home/user/folder1/" - Lists the contents of the /home/user/folder1 directory, putting / behind names of subdirectories.

  • mv filename target - Moves the file named filename to target.
    Example: "$ mv -i bookreport.txt public/" - Moves the file bookreport.txt to the public subdirectory. If the file already exists in target directory, then -i in the command asks the user to overwrite or not.
    Example: "$ mv /home/user1/*.jpg /home/user2/" - Moves all files that end with .jpg in the directory /home/user1/ to the directory /home/user2/ and will overwrite any files with the same names.
    Example: "$ mv /home/user1/image.jpg renamedimage.jpg" - The mv command can also be used to rename a file. This example moves the file /home/user1/image.jpg to the current directory and names it renamedimage.jpg. If renamedimage.jpg already exists, it is overwritten. Use -i for safety.

  • ps - Displays the information of all the processes you currently own. It shows several columns of information. Column 1 is the PID or process ID, TTY is the controlling terminal, STAT is the state of the process, TIME is the cumulated CPU time of the process, and COMMAND is the name of the process.
    Example: "$ ps a" - List all processes owned by you and have a controlling terminal.
    Example: "$ ps ax" - List all of the processes owned by you even if they don't have a controlling terminal.

  • rm -i filename - Deletes the file called filename. Be careful with this command, if you forget the -i part you will instantly delete the file forever.
    Example: "$ rm -i bookreport.txt" - Deletes the file bookreport.txt
    Example: "$ rm *.jpg" - Deletes all files that end with .jpg and DOES NOT prompt you to make sure you want to do that.

  • touch filename - Creates a file called filename that is empty. The file is saved in the current working directory. If the file already exists, it's timestamp will be updated to the current time.
    Example: "$ touch bookreport.txt" - Creates the file bookreport.txt


  • command &, Ctrl+z, bg num, fg num - Let's say I want to start Firefox to surf the web. In a terminal window, I could just type "firefox" to launch the program. But now I may want to copy some files in my home directory to another directory by using the terminal. If I switch back to the terminal window, I am unable to execute more commands because that terminal is running Firefox. So I must either close Firefox or open a new terminal window. The last thing I want right now is another window open, so.....let's learn a couple things that can help us here.

    If you want to run a command in the background and you know you want to do that before you execute the command, you can use an & sign. Placing the & sign after a command executes that command in the background.
    Example: "$ firefox &" - Starts firefox as a background process in the terminal so more commands can be executed. Firefox starts normally and can be used at the same time. Notice that after pressing enter on the command, a new line is printed in the terminal that looks similar to [1] 17844. The number in [] is process number 1 that you've put into the background of that terminal. The second number is the process ID.

    Now lets say Firefox was executed but not sent to the background (meaning the & sign was not used in the command) and new commands cannot be typed into the terminal window. I can move the running process to the terminals background by using two commands.
    Example: "Ctrl+Z" - In the terminal running the process you want to place in the background, you can press the keyboard combo Ctrl and z at the same time to suspend the process. The terminal will print a line like [1]+ Stopped firefox. Firefox is suspended and assigned a process number 1. Use the bg command to put that process into the background and resume it's operation.
    Example: "$ bg 1" - Places application with process number one into the background.

    To bring an application back to the foreground, we need to know it's process number. If you don't remember it, use the command jobs to print a list of all the background and suspended jobs. Then use fg to bring that job to the foreground.
    Example: "$ jobs" - Prints the list of background and suspended jobs. Example: "$ fg 1" - Brings process with process number 1 to the foreground.