Page 1 of 1

Beginner-Intermediate Bash Commands Rate Topic: ****- 1 Votes

#1 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Posted 17 May 2011 - 05:13 AM

*
POPULAR

From sheer boredom at 6am comes a tutorial.

A growing number of Linux users these days are going into the OS with little knowledge of some of the shell commands that make life much easier. While most modern gui-based systems have GUI-fyed applications to handle most of the things you can do in the shell, but being familiar with a

That said, let's get to some of the common and useful commands for the Borne Again (Bash) shell.

(as an aside, the reason I'm writing this for the Bash shell is because...I use the Bash shell.)

The Most Important Command
man
man is the manual command for Bash, typing man command-name gives a description of the use of the given command. It really is the most important command to know when learning how to do things via the shell, and since I suck at remembering flags, it is a command I use numerous times in any given day.

Basics

ls
ls lists all of the files in the directory you are currently in. Simply issuing ls will display the contents of the directory in a space-delimited list form. You can use "flags" (in essence, arguments prefixed with a hyphen (-)) to get some more information. Four common flags I use on a daily basis are as follows:

-l : List output - formats the output into a nice list format that displays the owner of the file, the date last modified, as well as some other useful data. (such as file permissions; more on that when we get to chmod)

-a : All. By default, ls will not display files that begin with a period, such as the .config file in your home folder.

-s : Size. Shows file size as well as listing the files

-t : Time. Orders the list by the last-modified time, from earliest to latest. (by default, the list is ordered in lexicographical order.)

And you can combine flags, the most common usage of ls I personally using being ls -last, a combination of the above flags.

Here is an example of what these various ls commands might look like:

Posted Image

cd
cd is the change directory command, it will change the directory the shell is in. You can use absolute paths here such as cd /home/user or use a relative path, ie. cd documents (if you are in a directory with a documents directory in it)

Two symbols of note that can be useful for the cd command are ~ and .. ~ signifies your home folder, so typing cd ~ will go to your home directory. (actually, just typing cd with no argument will bring you to your home directory as well). .. signifies the parent directory of the one you are currently in. For example, if I were to type cd .. in my home directory, I would move from /home/user to /home.

cp
cp copies files. The first argument is the file to be copied, and the second argument is the output file.

mv
mv "moves" files. It is also how you rename files via the shell (you move the file to a new file)

rm
rm removes files. Be careful about what you remove, as unless you use the -i flag, the files will be removed without prompting you.

cat
cat concatenates the contents of the file (or files) it takes as arguments and displays them on the screen. It's as simple as that, here is an example output:

Posted Image


touch
touch "touches" a file, modifying it's timestamp. Therefore, if you touch an existing file, you change it's timestamp, however if you touch a file that doesn't exist, touch will create the file. It will be completly empty, however.

top
Top provides a constantly-updating (you press q to exit it) "system status", which lists the top processes in terms of hardware usage, and more importantly for the next command, it gives process IDs for the processes.

An example of what it may look like:

Posted Image

kill
kill sends signals to the process you specify. These signals take the form of numeric flags, so an example of a kill command would be kill -9 2848.

There are a lot of signals, but from personal experience, I only use three, -1, -2, and -9

-1 is HUP, the hangup signal. This is this is the signal that is sent to any running processes when you close the shell (using kill -1 doesn't close the shell, however).

-2 is INT, the keyboard interrupt. It is the equivalent of pressing Ctrl+C with a program running in the shell (but you can do it to programs running in the background as well)

- 9 is KILL, the sure kill. You kill the process. Period. Only zombie processes cannot be killed by -9. (zombie processes are beyond the scope of this writing)

Here is an example:

Posted Image

In terms of general use things you'd use on an average day, that's all I've got for the basic stuff, let's move on to some more intermediate things.

Pipes
The concept of a pipe, at least in terms of the shell, is to use the output of one command as the input for the next one, for instance the following line:

cat /etc/passwd | less


Will cat the file /etc/passwd (it stores information about the various 'users' of the machine) and then use the output of that command as the input of the less command (less is a command that makes large files easier to read by allowing you to scroll up and down in them)

That's really all there is to pipes, at least in terms of what you would do with them on a daily basis.

Redirection
An important topic of basic shell use is redirection. The >, >>, and < symbols are the key redirection symbols you'll be using most of the time. > and >> are output redirection, they redirect the standard output location of a command (generally the monitor) to a file. For example, the following command directs the results of the echo command (which "echos" its arguments back to you), and instead places the output you would normally get on the screen into a file:

Posted Image

The difference between > and >> is that > is overwrite and >> is append. Using > will overwrite the output file if it exists, where >> will append to the end existing files.

< is input redirection, generally I don't use this as much as I do output redirection, but it's there, and its functions similarly to output redirection.

Error Redirection
In the Bash shell, aside from redirecting standard input (usually the keyboard) and standard output (usually the monitor), you can also redirect where error messages go using the 2> notation in the line. (as for an explanation of the 2, standard input, output, and error can be referenced by numbers when using redirection, 0, 1, and 2 respectively. Technically speaking, you can notate standard output as 1> instead of >, but that is redundant.)

Look at this example:

Posted Image

The first time I use the bad command without any redirection, and the error is displayed on the standard error device (the monitor). However, the second time I do it, I redirect the error output to a file instead of the standard error output. This is useful for creating error logs.

If you wish to just suppress error messages, you can direct the error out to the /dev/null file. The null fill is...null. If you direct output or error to it, they are "suppressed"; they don't display on the standard output/error and they aren't written anywhere. If you direct input from /dev/null, you get...well, null, nothing. Not as useful for everyday things as output/error redirects, but it still has it's uses.



And that's all I can think of off of the top of my head for commands I use on a daily basis, hope it helps.

Is This A Good Question/Topic? 10
  • +

Replies To: Beginner-Intermediate Bash Commands

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Posted 17 May 2011 - 07:19 PM

*
POPULAR

It may also be good to note that multiple commands can be separated with a ;
Ex:
echo hello > out.txt; cat out.txt

This post has been edited by H3R3T1C: 17 May 2011 - 07:24 PM

Was This Post Helpful? 5
  • +
  • -

#3 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Posted 17 May 2011 - 07:52 PM

*
POPULAR

Also that if you use ampersands instead of semi-colons that it executes things in parallel rather than sequentially, which the semi-colon does.

So for example:

date; echo meh

will do date first, then echo when it finishes date, whereas,

date & echo meh

will execute the two commands at the same time.

Thanks for making me remember that D:
Was This Post Helpful? 8
  • +
  • -

#4 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Posted 17 May 2011 - 08:05 PM

LOL I totally forgot to mention the ampersand when I was replying with the semicolon.

This post has been edited by H3R3T1C: 17 May 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Posted 18 May 2011 - 08:21 AM

Thanks!

Some of these I didn't know and some of them I simply can't remember, but this is useful either way. ^^

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 18 May 2011 - 09:40 AM

Good introductory tutorial. I'm sure it'll help a lot of people who are just starting out in Linux. :) Maybe it deserves pin topic in that area?
Was This Post Helpful? 0
  • +
  • -

#7 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Posted 18 May 2011 - 02:33 PM

Meh, if it were to be pinned, I'd want to flesh it out some more. There's some stuff I didn't mention, looking through it, I completely forgot to mention sudo, chmod, and a few other commands, and how they can be used (such as chmod 000 file(I do it old school) + sudo nameoffileviewer to prevent people from looking at folders if you forget to lock it, as well as why that would word on a Linux machine but not a UNIX one.)
Was This Post Helpful? 0
  • +
  • -

#8 Erudite  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 51
  • Joined: 20-April 11

Posted 18 May 2011 - 02:48 PM

Good introduce to bash commands, but of course we can't call it intermediate... Basics...
Was This Post Helpful? 0
  • +
  • -

#9 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Posted 18 May 2011 - 02:57 PM

The base level of new Linux users is moving slowly, but surely, to the point where one could argue that redirection is no longer something "basic", at least imo.

This post has been edited by Tarkenfire: 18 May 2011 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#10 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Posted 18 May 2011 - 07:19 PM

Very nice! Being a new user to Linux myself, I liked the way you presented the information. It was easy to follow. And I knew most of the basics you taught, which somewhat surprises me.

What distro do you use?

This post has been edited by Gamegoofs2: 18 May 2011 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#11 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Posted 18 May 2011 - 09:24 PM

View PostGamegoofs2, on 18 May 2011 - 07:19 PM, said:

Very nice! Being a new user to Linux myself, I liked the way you presented the information. It was easy to follow. And I knew most of the basics you taught, which somewhat surprises me.

What distro do you use?


Well, the one seen up there was LinuxMint, an unofficial spinoff of Ubuntu. My netbook runs Kubuntu, and I've run Ubuntu, Fedora, and Debian boxes as well before.
Was This Post Helpful? 0
  • +
  • -

#12 mkoop  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 17
  • Joined: 14-April 11

Posted 25 October 2011 - 08:30 AM

apropos is also useful when learning. If you don't remember all the man commands, you can type:

apropos keyword

for example

apropos copy

This will list all man commands that include the copy keyword.

It is also good to note that if you see for instance Intro(3) that is because there are 3 (or more) sections of the Intro main page. To display sections use the syntax:
man 3 intro.

With the man and apropos commands and a bit of logic you will learn exponentially faster.
Was This Post Helpful? 0
  • +
  • -

#13 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Posted 10 July 2012 - 03:25 PM

Nice, I could use this as a review of some of the stuff I first learned a while back =D.
Was This Post Helpful? 0
  • +
  • -

#14 Nitewalkr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 17-November 10

Posted 17 December 2013 - 11:00 AM

View PostTarkenfire, on 18 May 2011 - 02:33 PM, said:

Meh, if it were to be pinned, I'd want to flesh it out some more. There's some stuff I didn't mention, looking through it, I completely forgot to mention sudo, chmod, and a few other commands, and how they can be used (such as chmod 000 file(I do it old school) + sudo nameoffileviewer to prevent people from looking at folders if you forget to lock it, as well as why that would word on a Linux machine but not a UNIX one.)


EDIT: Sorry I forgot that nano was not a bash command. :D heh heh.

Anyways for the rest of the bash commands this might help make this tutorial interesting discussable:

http://ss64.com/bash/

This post has been edited by Nitewalkr: 17 December 2013 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1