Friday, June 24, 2011

A Quick Overview of Linux Commands [Linux]

Before my posts really start, I thought a quick overview of commonly used Linux commands would be useful since this blog is for complete newbies and those starting off with almost no experience.

Below is a list of commonly used Linux commands (I will update this frequently, so it might be small at first):

  • NOTE: know the difference between "absolute" and "relative" path names? Absolute path names are used when you type a "/" (slash) before the pathway (E.G. typing ls /pentest/exploits/ will print out that from your main Linux directory), but using a relative pathname references your current directory's subdirectories (E.G. typing ls pentest/exploits/ would print out that directory if it exists in your current directory).
    I'll try to explain this more in depth with better examples, but a quick rundown can't necessarily hurt. You can think of absolute path names as never changing and relative referencing things that are relative to your current position! (quite simple once you get it)
  • man --- the manual command. Type "man [any command]" and it will bring up a manual to that command if there is one (which there usually is). This is probably the best command for your learning experience. Abuse it and use it to help learn each command and their "-" options. 
  • cd --- stands for "change directory. Navigates you to the specified directory OR moves you to your "home" directory if no directory is specified. E.G. "cd" alone would move you to /home/username/ or "cd etc" would move you to the "etc" directory that is located in your current directory, but "cd /etc" links to an "absolute" path name which is a standard Linux directory.
  • pwd --- stands for "print working directory" (thanks Anon in comments!) prints out your current directory, it's as simple as that; "makes it easier to remember for someone new."
  • ls --- lists out files and sub-directories in your current directory.
    • Using the sub-commands "-al" lists all (the "a") and in long format (the "l"); makes it easy to see hidden files and directories and also an easier to read format. Requires the dash "-" before the "al" and a space before it in between the "ls" and "-". E.G. "ls -al" in your home directory would print all files and folders (including hidden), then you can move to them or view/edit.
  • cat --- lists the file out on your screen in the terminal. Only works with files that are readable; if you try it with a directory, it will output an error.
    • I'll copy the helpful Anon from my comments, since he explained this quite well: "cat requires one or more filenames as parameters. The original purpose of cat was to concatenate files E.G. "cat file1.txt file2.txt > file3.txt" will create file3.txt that contains the contents of file1.txt followed by the contents of file2.txt."
    • This is very helpful information for new users trying to understand how to concatenate and manage files. The ">" operator takes the first two files and "pushes" them into the "file3.txt." Usually when operators such as ">" and "<" are in play, the direction they point is important; below I'll review the ">" and ">>" operators in more depth.
  • emacs --- a Linux text editor in your terminal; I will provide more help with this in a later post. You can edit a file with the command "emacs [file location or name if in same directory]" and play around with it.
  • vi --- another Linux text editor.
  • nano --- my favorite Linux text editor so far; easy to use and pretty self explanatory. I don't believe it is built in to classic Ubuntu though.
  • grep --- stands for general regular expression print; it searches through a file (or an output as I will show and explain) for a certain string or other options.
    You use this tool, for example, to search through a huge directory looking for a specific file, to see if it's located in there. Type "ls -al | grep [folder name]" to see how it works. The "|" is called "piping" and I will cover this next.
  • | --- this little tool is used when you want to "pipe" commands. Piping is, simply put, is running more than one command at once into one command. For example, eventually I'll show a quick BASH scripting guide and we might do something like "ping -c 1 | grep "bytes from" &" which I will explain in depth later on. All you need to know is if you want a general command (such as an nmap or ping command, but want to grep or ls or cut out certain things, just pipe in that command afterwards each with their own "|" after.
  • ping --- stands for Packet InterNet Groper... yes, groper. It basically "gropes" the specified internet source (whether it be by name on a local network, or a website name, or by IP address). It uses an "echo" system with acknowledgements that packets were sent and received to determine if hosts are "up" (able to be connected to).
    • Some options (like the one used above explaining piping) can be added on. For example, the ping command keeps pinging until you stop it (ctrl-z is the EOF or end of file command, and stops most running programs in Linux), so to only ping 3 times, you would add the "-c 3" command beforehand (in our example I used 1 just to ping it once).
    • Typing "ping --help" into the terminal brings up all the options you can use; try playing around with a bunch of them by pinging or "localhost" which is yourself. Another way to ping yourself is use the "loopback" IP which is, or you can ping any other IP.
  • cut --- this command cuts out selected items from a file or output (it can be used with piping and a ping command which we will do in later posts). Typing cut --help brings up the help menu for this command, or try "man cut" to read more about it. It's a very useful tool to use when scripting in BASH.
    • I'll update this section with an explanation and more helpful information for newbies soon!
  • echo --- echo literally echos back what you type. For instance, if you type into a terminal "echo hello" it will print "hello" below. This is used in scripting a lot and you should understand how simple it really is.
    • Getting a bit more technical, you can echo certain "variables" that Linux has, such as the hostname, IP, and other things we will get into later. Try the command "echo $HOSTNAME" and see what comes back. It should be your username you have logged into. Cool, right? This is known as an environmental variable and is useful while creating scripts and user friendly interfaces later. I'll cover environmental variables and more helpful information on this in another post with BASH scripting. If you don't understand the whole "$HOSTNAME," it doesn't matter yet!
    • To get the help page for this, the command "echo --help" doesn't work. It will echo back "--help" which is annoying. Use the "man" command by typing "man echo" and read up on this useful Linux command.
  • arp --- check out my ARP post.
  • touch --- creates a file with the name you wish in your current directory. For example, typing "touch file.txt" would create the file with the name "file" and the extension ".txt"
    Now for some Linux maintenance, updating, and application downloading/installing:
    • apt-get --- [my] classic command for getting applications on Ubuntu. Each version of Linux has it's own patience package which at the moment I am not familiar with, but I will attempt to update as I learn them; below is some commands that are useful; append them after a space to this command. E.G. apt-get [commands].
      • install [application name] --- self explanatory; it installs (and prompts if necessary) the application you have entered.
      • upgrade --- Upgrades all your packages (or programs) that have available upgrades; definitely useful to run once in a while.
      • update --- Retrieves the list of packages that are available for your system to upgrade and install; also useful to run every once in a while.
    • apt-cache search [string (or keywords)] --- searches the application database with your string or keywords for applications. Very very useful if you want to find certain programs to install.
      Again, the "apt---" are for Ubuntu; each flavor of Linux has it's own maintenance packages, and each has multiple.
    Try using these commands by typing "apt-get install ssh" and looking at the output; ssh stands for "secure shell" and is a way for us to access other computers. There is also "ftp" which stands for "file transfer protocol" and is a way for us to transfer files from computer to computer through the terminal (and also user-friendly GUIs).

    Of course these are all simple, and I will be adding more and more to them as I remember/discover new ones, so don't be alarmed when there's only basics up at the moment.  As always, ask questions below and I'll get back to you!

    Last updated: September 24th, 2011.


    1. I'd like to make some comments with the understanding that these are meant as constructive criticism and are not meant to be nitpicky or aggressive.

      - "cd etc" would move you to the "etc" subdirectory of the directory you are currently in, if there is one. "cd /etc" would move to the Linux standard /etc directory.
      - pwd stands for "print working directory," which makes it easier to remember for someone new.
      - cat requires one or more filenames as parameters. The original purpose of cat was to concatenate files, e.g. "cat file1.txt file2.txt > file3.txt" will create file3.txt that contains the contents of file1.txt followed by the contents of file2.txt. The most common purpose of cat nowadays is to output the contents of a file to stdout.
      - grep: your explanation seems a bit inaccessible to a newbie. First of all, it does not, by default, search through a file. It can, but that's a command line parameter. One of the more common uses of grep is to pipe input to it (such as the output of ls or cat) and filter. In your example with piping input to grep, you are assuming the reader's knowledge of how to pipe/redirect output on a *nix command line, which the beginner most likely does not know of or understand. You should at least move your comment about the pipe character to above grep, though it would probably be best to handle |, >, and >> in their own section.
      - cut: I think you should explain more about cut. You said that it "cuts" data, but that doesn't mean anything to anyone that doesn't already know it. To make this more accessible to people new to *nix CL, I think that you should give an example of getting characters 4-10 from input piped to cut from echo, or something similar.
      - echo: I think that your mention of echo $HOSTNAME is a poor example before mentioning environment variables.
      - apt-get: this is not a general Linux "maintenance" command. It will work for Ubuntu or Debian, but what about Arch/Slackware/Gentoo/etc? I think that you should introduce apt-get by saying that you're using it since you're running Ubuntu for what you'll be talking about in this blog, with a brief mention about other distro's equivalent package managers.
      - apt-cache: same as for apt-get.

    2. @Anon, thanks for the help, I'll update this in a bit with all the changes I see fit. About your apt-get comment, I'm definitely going to keep updating this with more information about other builds, but for now I'm just keeping it basic. Thanks for the feedback!

    3. FYI:
      1. FTP is File TRANSFER (not transport) Protocol.
      2. pwd is Print WORKING (not wording) directory.
      3. You say you'll explain > and >> in detail below but then never do.
      4. I can't find anything to back up your claim that ping is a acronym- maybe you were joking? As far as I know, it's a reference to the 'ping' sound that submarine sonar makes when detecting object. The creator of the ping command says so here:
      5. Your explanations of apt-get upgrade and apt-get update don't make sense. These commands don't "update your apt-get", whatever that's supposed to mean. 'update' retrieves the list of packages available for the system to install/upgrade and 'upgrade' performs the upgrade of all packages for which newer versions are available.

      Other than these nit-picks, great blog!

    4. @Tim, thanks for the updates, I'll change those right away; Also, I mean I'll explain ">" and ">>" eventually. I'm on vacation right now trying to get a bunch of things done, so I'll probably have them and a lot more explanations done next week ;D
      As for the Ping acronym, yes, it really does stand for Packet InterNet Groper AFAIK (I can find reference online and as well as in books).
      Thanks for the information!

    5. I appreciate your blog as I am starting my journey into learning all that I can about Linux before I move on to more complicated Computer/Hacking subjects, ie. Learning to program in Python, Networking, Socket Programing, ect. Think you will touch on any of these in the future?!?

    6. @Anon, Yes, I will definitely cover Python and Perl scripting, what do you mean by "networking" though? It's a very broad term, but I'm planning on going in depth about most networking involving Windows/Mac/*nix.

    7. This is a nice list. Short, sweet, and to the point. I'd like to see more, since I'm extremely new to this kind of thing and would really like to learn more.

    8. @Breffmint, I'm writing up more posts at the moment, thanks for the comment!

    9. blind leading the blind

    10. @Anon, better than the deaf and blind leading, right? haha.

    11. Hey man I think what your doing is great. I'm sure as you get used to posting, as with anything else, the quality will get better too. I have a feeling there's going to be a lot more information coming and I can't wait to see it.

      1. Thanks T-Can! I'm getting back into it right now, but it's been a while!

    12. This is good stuff. All those who have posted negative feedback aren't really the people who should be reading this anyway. I am fairly new to linux, been running it for about 6 months, and I have found all your posts helpful. The problem with the majority of forums/tutorials about linux is that they assume you know a lot about linux. I notice you haven't posted in a while, hope you haven't given this up.

      1. Thanks Stanley! I'm actually getting back into it recently, so I hope I can keep up with the posting since I've learned a massive amount of stuff that I'm sure everyone would love to read.

    13. for beginners like me need a lot of reading and searching for information on various blogs. and articles that you share a very nice and inspires me .
      cara menggugurkan kandungan
      obat aborsi
      kalkulator kehamilan


    14. ارخص شركة نقل عفش بالدمام شركة نقل عفش بالدمام
      شركة تنظيف منازل بجدة بالبخار شركة تنظيف منازل بجدة

    15. This comment has been removed by the author.

    16. Wow What A Nice And Great Article, Thank You So Much for Giving Us Such a Nice & Helpful Information about Java, keep sending us such informative articles I visit your website on a regular basis.Please refer below if you are looking for best Training Center.
      Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery