Wednesday, February 24, 2010

Debian 5

Basic Linux commands are typically universal over all Linux distributions. In GNOME's terminal, you can use CTRL+T to open a new tab. The default shell in Debian and most other Linux distributions is the BASH shell.
The BASH shell has 2 prompts: $ for non-privileged (non-root), # for privileged (root). An example of a non-privileged prompt is:

To find out who you are for a given shell, use the command:

tty stands for Teletype. To find out what terminal are you currently logged on to, type:

When using GNOME, it automatically creates a pseudo terminal. An example pseudo terminal will look like /dev/pts/0. The number counts increntally by 1.

To reveal the currently connected users, their ttys and programs, use:

The w command also tells us what's last run in a particular tty.

A command similar to w but a little simpler is:

To find out where we are in the directory tree relative to '/', use:

pwd prints the working directory. The '/' is known as the absolute root. pwd is actually retrieved from an environment variable '$PWD'. '$PWD' is updated as we navigate throughout the directory tree. BASH updates the '$PWD' variable. There is also another variable maintained called '$OLDPWD' which stores where we were before we were in '$PWD'.

To change directories, use:

cd can accept relative paths or absolute paths. Relative paths are paths relative to the current folder. Absolute paths begin with '/' and is the absolute path with respect to the absolute root. Absolute path behaves the same no matter where you are in the directory tree, while relative depends on where you are. Executing cd with no parameters or cd ~ takes you back to the home directory.

To create file or update a file's timestamp information, use:

You can use "touch test.txt" to create a blank file, or touch followed by an existing filename to update its timestamp. Zero-byte files are required by some scripting applications.

The equivalent of dir in Linux is:

The parameter -l does LONG listing, which includes permissions, links, owner, size, modified timestamp, and file/directory name in the output. The -a option lists files that begin with a ".". Files that begin with "." are hidden files. You can also do sorting with ls. For example, the -ltr would sort by modified time. -t means to sort by time, and -r is to reverse the order.

To find files in the current directory with ".txt" in the filename, use:
find ./ -name '*.txt'

Find also finds recursively into the folder's sub-directories.

echo prints text onto the console. It can also allow you to do output redirection into a file like this:
echo "Hello world!" > helloWorld.txt

With echo, you can also print the contents of a variable. For example, $? stores the exit status of the most recent command. To see its contents:
echo $?

A 0 return typically means that an operation has succeeded.

cat is for concatenation. It concatenates the contents of text files. If a single file name is specified, it will print it on the screen:
cat test.txt

However, we can also use cat to print multiple files. We can also do output redirection into a new file, like this:
cat test.txt test2.txt > test3.txt

touch is used to create blank files. To create directories, use:
mkdir temp

To delete directories, use:
rmdir temp

Directories must be empty before it can be removed.

To display all defined aliases, use:

The same command can also be used to create your own aliases:
alias dir='ls -l'

Now, when you use dir, it will execute the LONG listing. Aliases are only available for the current tty or pts you're in.

To see a list of commands you've used, run:

By default, it stores the past 1000 commands. The history is stored sequentially in .bash_history in the home directory.

If you use history, you can see the command numbers. To run the 50th command, we can use:

! is called "shbang" in Linux.

At times a user may want to remove all program output from the screen. We can do this using:

To remove files, use:
rm test.txt

rm can also remove directories. Directories in Linux are considered special files. You can recursively remove directories and all its contents using the "-rf" parameter.

To copy a file from source to destination, use:
cp source destination

cp by default has no output. To make it output its operations, use the "-v" parameter which stands for verbose.

To display a file's path in the $PATH variable (if it exists), use:
which filename

filename can be replaced with a program name. For example, if we run "which ls", we will see "/bin/ls".

When ls is run in auto-color mode, directories will be blue and applications will be green.

To move or rename an item, use:
mv source destination

To rename test.txt to testing.txt, you can use "mv test.txt testing.txt". Moving or rename a file will not change the timestamp unless you cross file systems.

BASH supports command-chaining through the ; delimeter. An example of command-chaining is:
ls -l; mv -v test.txt testing.txt; echo 'Exit Status: $?'

BASH also supports logical AND and OR. An example application is:
clear && ls -l && mv -v test.txt testing.txt && echo 'Exit Status: $?'

The above means that the clear must be successful before the ls -l will be executed, and the ls -l must be successful before the mv will be executed.

Logical OR in BASH is different from in programming. OR runs through the list till a command works. For example:
echo Fish || echo Toilet

This will only show "Fish". However, if we modify it to "echotoot Fish", echotoot will fail, and "Toilet" will be shown.

If a command's output is too long, pipe it to more or less, like this:
ls -l /var/log | more

This process is known as pagination. "less" is newer than more in that it provides the ability to scroll back up. In "less", use "f" or SPACE to go forward, or "b" to go back. Use "q" in more or less to quit. Piping sends the output of one process to the input stream of another. You can do multiple piping.

Ouput redirection is invoked using the ">" sign which redirects the output of a process into a file. To append to a file, use ">>". It will add on to an existing file, or create a new file if the file does not exist.

If you only want to see the top or the bottom of a file, you can use "head" and "tail" commands like this:
head -n 5 test.txt

The above command returns the top 5 lines of a file. Change head to tail and it will return the last 5 lines. By default, if -n is not specified, it returns 10 or 15 lines depending on the Linux distribution.

You see the file's MIME type, use:
file test.txt

To do a word count or line count from a text file, use:
wc -l test.txt

If the -l is not passed in, it returns the number of line, words, and characters respectively.

To quickly generate a sequence of numbers, use:
seq 1 10
seq 10

Both syntax will return:

Sequence is typically used to drive BASH loops.

Outputting to STDOUT (the console screen) is typically much slower than outputting to a text file.

To check the disk utilization throughout a directory structure, use:
du -ch

Without the -ch, the output would be non-human-readable.

No comments :

Post a Comment