1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Linux Shell Basics

Discussion in 'Papers' started by Stéphane Lenclud, Apr 2, 2017.

  1. Stéphane Lenclud

    Stéphane Lenclud Founder Staff Member

    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:

    Code (Bash):
    1. #!/bin/bash
    2. folders=`find -xtype d`
    3. for folder in $folders ; do
    4. chmod -v 775 "$folder"
    5. 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:

    Code (Bash):
    1. 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:
    Code (Bash):
    1. 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:

    Code (Bash):
    1. find . -type f -regex .*\.conf -exec grep -H -i LoadModule {} \;
    Find all files with a name matching LeftBar but not matching WebLeftBar:
    Code (Bash):
    1. find . -name "[b]LeftBar[/b]" ! -name "WebLeftBar*"
    Find all files with a name matching favi* and display their properties:
    Code (Bash):
    1. find . -name "favi*" -exec ls -l {} \;=


    Want the download a file? Just use:
    Code (Bash):
    1. 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:
    Code (Bash):
    1. 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:
    Code (Bash):
    1. apt-cache search <string>
    • To install a package:
    Code (Bash):
    1. apt-get install <pkg-name>
    • To list installed packages:
    Code (Bash):
    1. dpkg -l
    • Get the version of appach2 installed on your machine:
    Code (Bash):
    1. dpkg -l | grep apache2

    Symbolic link

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

    Code (Bash):
    1. ln -s /etc/apache2/mods-available/auth_ldap.load

    Create an empty file

    Just do:

    Code (Bash):
    1. touch <filename>

    To do deltree

    Code (Bash):
    1. 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:
    Code (Bash):
    1. gzip -d MyFile.tgz
    • Then using tar:
    Code (Bash):
    1. tar -xf MyFile.tar

    Memory information

    Code (Bash):
    1. cat /proc/meminfo

    Run all daily cron jobs

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

    Free disk space

    Code (Bash):
    1. 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:
    Code (Bash):
    1. export PS1" \u@\h:\w\$"=
    2. export PS1"\h:\w #"=

    Send file through FTP

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

    Process status

    See PS command.
    Code (Bash):
    1. ps -Al
    The column UID gives you the user ID for processes. To get the corresponding user name do
    Code (Bash):
    1. cat /etc/passwd.

    Kernel module

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

    Date display

    Try running:
    Code (Bash):
    1. date -R


    Last edited: May 30, 2017

Share This Page