Linux Shell Basics

On that page you find some tips on how to do things from the command line on Linux.

Batch chmod

I'm no Linux crack, so I won't be able to tell you how many variants there are of Shell languages out there. In fact I don't even know what version of Shell I've been using myself.

I had to change some file permissions the other day when deploying some web site. To do that I used the following shell script:

Bash:
#!/bin/bash
folders=`find -xtype d`
for folder in $folders ; do
chmod -v 775 "$folder"
done

It gets the list of sub-directories put it in a variable and then executes a chmod for each of them. Later on I find out that I could have done the same thing in one single line:

Bash:
find . -type d -exec chmod 775 {} \;

I haven't tried that yet though.

Find command

Find files in the current directory that do not contain '.' character besides the first character:
Bash:
find . -maxdepth 1 -type f -regex '\.[^\.]*'

Find all the file from the current directory with .conf extension and containing the string "LoadModule".
Grep flag -H prints the name of the file and -i flag makes the search case insensitive search:

Bash:
find . -type f -regex .*\.conf -exec grep -H -i LoadModule {} \;

Find all files with a name matching LeftBar but not matching WebLeftBar:
Bash:
find . -name "[b]LeftBar[/b]" ! -name "WebLeftBar*"

Find all files with a name matching favi* and display their properties:
Bash:
find . -name "favi*" -exec ls -l {} \;=

Downloading

Want the download a file? Just use:
Bash:
wget <url>
Here again thanks to Daniel.

Debian Aptitude

I had to use that to install missing packages while setting up TWiki on a Debian machine.
  • To show available packages:
Bash:
apt-cache show <pkg-name>
Where pkg-name is an exact package name
  • To display a list available packages containing a string in their name or description:
Bash:
apt-cache search <string>
  • To install a package:
Bash:
apt-get install <pkg-name>
  • To list installed packages:
Bash:
dpkg -l
  • Get the version of appach2 installed on your machine:
Bash:
dpkg -l | grep apache2

Symbolic link

To create a symbolic link with the same name as the original final in the current directory:

Bash:
ln -s /etc/apache2/mods-available/auth_ldap.load

Create an empty file

Just do:

Bash:
touch <filename>

To do deltree

Bash:
rm -rf <dirname>

Console text editor

  • Bad old Vi .
  • fte: I used it on a Debian it was ok but nothing exciting.
  • mc: Midnight Commander a clone of Norton Commander. It's rather good and does syntax highlighting for my Perl scripts.

Handle TGZ file

  • First deflate using gzip:
Bash:
gzip -d MyFile.tgz
  • Then using tar:
Bash:
tar -xf MyFile.tar

Memory information

Bash:
cat /proc/meminfo

Run all daily cron jobs

Bash:
for f in /etc/cron.daily/*; do echo $f; $f; done

Free disk space

Bash:
df -hl
df means diskfree
-h means human readable
-l means local disks only

Debian shell prompt

To display the working directory in your shell prompt:
Bash:
export PS1" \u@\h:\w\$"=
export PS1"\h:\w #"=

Send file through FTP

Do something like:
Bash:
scp ~/localdir/my.file user@example.com:~/ftpdir/my.file

Process status

See PS command.
Bash:
ps -Al

The column UID gives you the user ID for processes. To get the corresponding user name do
Bash:
cat /etc/passwd.

Kernel module

We are using the psmouse module as an example.
Get information about a module :
Bash:
modinfo psmouse
Remove a module :
Bash:
modprobe -r psmouse
Add a module :
Bash:
modprobe psmouse
Add a module with argument :
Bash:
modprobe psmouse proto=imps
List modules :
Bash:
lsmod

Date display

Try running:
Bash:
date -R

References

 
Last edited:
Back
Top