GNU/Linux Tips, Tricks, and Techniques
Ernesto P. Adorio
With GNU/Linux, we have a combination of a free operating system kernel and software
tools based on the solid commercial Unix. Companies, educational and government
institutions and even students are discovering the economy, power and productivity
gains in using GNU/Linux (shortened to Linux from hereon). Linux can be installed in
old hardware and is fast, reliable and less susceptible to computer viruses. Students
would be encouraged to study and use a 'real' operating system since the source codes
are available.
Extensive help for doing work in the Linux environment is available from the web that
we are encountering the unusual problem of information overload! Linux users should be
trained to employ search engines intelligently to zeroin on relevant information.
In this article, we present a few useful tips, tricks, and techniques for productive work with Linux.
Help in Linux: info, man and apropos
Every Linux user should be familiar with the info and man commands.
If you want detailed help on the ls command, type info ls and you will be
presented with the documentation for that command.
Type 'q', without the quotes to quit, '/searchstring', again without the quotes,
to find occurrence of searchstring in the current page. The following navigation keys maybe
also be available when the info file is large: 'N' for next page, 'P' for previous page, 'U'
for top page. There is usually a keyed menubar shown on top of the info page.
To find all man pages containing a word, type apropos word.
Bash shell commands
Here is a listing of some bash shell commands:
- Changing your password with passwd, ypasswd
Type passwd to change your password.
- Copying files with cp
To copy a single file f1 to f2: cp f1 f2
To copy all files in current directory to a directory named d1: cp * di/
To copy all files recursively: cp -R * d1/
- Renaming files with mv
To rename file f1 to f2: mv f1 f2
- Copying files to a remote machine with scp
To copy file f1 in current local machine to user home directory in
remote machine named master:
scp -p f1 master: /
- Removing files with rm
To delete file(s) f: rm f
To delete file(s) f with safe prompt: rm -i f
To delete files(s) f recursively: rm -r f
To delete all files when you are root (It's the end of the world!): rm -rf /
- Displaying and concatenating files with cat
To concatenate files (display) f1, f2, f3 to console: cat f1 f2 f3
To concatenate files f1, f2, f3 to file f4: cat f1 f2 f3 > f4
To append files f1, f2, f3 to file f4: cat f1 f2 f3 >> f4
- Browsing a text file: less fname or more fname
- Finding files with find and locate
To find all locations of a file f1 using find: find / -iname=f1
To find all locations of a file f1 using locate: locate f1
- Determining the file type with file
To see what type of file is "/bin/sh": file /bin/sh
- Listing directories and files with ls
|
| ls * | List all files and directories |
| ls -R | List all files and directories recursively |
| ls -l | List all files in current directory with full information |
| ls -al | List all files including hidden files (starting with .) |
- Creating new directories with mkdir or md
To create a directory named happy in the current directory: mkdir happy
To create a diretory named tmp in your home directory: mkdir /tmp
To create a directory named tmp in a directory named other:
mkdir toto/tmp
- Changing the currend directory with cd
To go back to your home directory: cd
To go back to previous working directory: cd -
- Removing a directory and all its files with rm -if
DANGER! To remove a directory named tmp: rm -if tmp
- Get current working directory with pwd
- Getting to know who is logged on with finger, user
- Knowing the name of your machine: uname -a
- Timing programs with time:
To time a program named prog: time prog
- Getting and setting system time,date with date
- Job control with bg and fg:
To background a running program named prog which you own: bg prog
- List all process: ps aux or top:
- Changing file permissions with octal codes using chmod:
File permissions maybe coded as coded as a 9 bit octal number.
The bits are grouped into three's in the order of (from the left)
owner bits, user group bits and others bits. The 3 bits denote the
read, write and execute flags.
You can see the current permissions of a file f using ls -l f.
To change the permissions of file f to "rwxr-x-x", the octal code is
is (4+2+1) (4+1) (1) or 751: chmod 751 f
- Changing ownership of file(s) with chown:
To change ownership of file f1 to user jerry chown jerry f1
To change ownership of file f1 to user jerry who belong to group devel:
chown jerry.devel f1
- Determining,setting and saving environment variables with echo, env, export
Listing all environment variables:
env
Printing the value of a known environment variable name:
echo $HOME
Setting an environment variable to a new value:
export PATH=$PATH: /newdir
export PATH= /newdir:$PATH
To save your modified environment variables:
Edit your .bashrc and .bash_profile files to insert or change
the export commands.
- Checking internet connections with ping
To determine if there is path to yahoo.com:
ping http://www.yahoo.com
- Determining active LAN computer nodes with ping
ping 224.0.0.1
- Input/output redirection with <, <<, >, >>
To redirect the output of program prog to a file out.log:
prog > out.log
To redirect console input to a file in.dat:
prog < in.dat
To read input from file in.dat and output to a file out.log:
prog < in.dat > out.log
- Using pipes with mkfifo
To create a FIFO file named fife: mkfifo FIFO
History/Readline for productive console work
Your system should have the readline/history package installed if
you want to be productive with the command line interface. We
list the following commands when readline is available.
| Up arrow | Previous stored command |
| Down arrow | Next stored command |
| Ctrl-R | Perform search for previous commands |
Passwordless ssh
Assume that you are using a local machine and you wish to log-on to the
remote machine where you have an account without being asked for your password.
This is a necessity if you are doing parallel computing.
If you have not done so, type the following command:
ssh-keygen -t rsa
This will generate your hidden key file id_rsa and public key file
id_rsa.pub in your $HOME/.ssh directory.
Be sure to set the proper file permissions of the directory and files:
chmod 0755 $HOME/.ssh
chmod 0644 $HOME/.ssh/*
Next, copy the id_rsa.pub file to the remote machine:
scp -p id\_rsa.pub remote:~/.ssh/authorized_keys
The next time you ssh to the remote machine, it will not ask for your password. If this does not work, ask your friendly SA (system administrator).
If you wish to log-in to the remote machine from other terminals, perform the same
procedure except that instead of copying the id_rsa.pub file to the remote authorized_keys file, you append it.
C Programming
Compiling and running C programs
Assume we have the following input file named hello.c
#include <stdio.h>
int main(int argc, char **argv)
{
printf("hello world.\n")
}
To compile to default executible file a.out:
gcc hello.c
To compile to named output file hello:
gcc -o hello hello.c
To compile only to object file:
gcc -c hello.c
To show all compilation warnings (Highly recommended!):
gcc -Wall -o hello hello.c
To generate an assembly file:
gcc -S hello.c
If a source file myc.c calls a function in the C standard math library,
then the math library must be linked:
gcc -o myc myc.c -lm
If there are object files obj1.o, obj2.o to be linked:
gcc -o myc myc.c obj1.o obj2.o -lm
Last updated Jan 13, 2004.
File translated from
TEX
by
TTM,
version 3.38.
On 20 Jan 2004, 21:19.