Bash Commands

Jump to: navigation, search

SEE ALSO: Samba Config for Linux

Basic stuff


Reboot immediately

shutdown -r now

Shutdown immediately

shutdown -h now

Get Information

Show Linux Distribution and version

cat /etc/*-release

Check if CentOS is x86 or x64 (run the following command, and if "lm" is present it's x64)

grep flags /proc/cpuinfo


Find file by name

find / -name central.php

Find file containing text string (search recursively through sub-directories)

grep -r "16.132" /etc/bacula/conf.d/


Show current directory


Move up one directory

cd ../

File Manipulation

List files (with details)[1]

ls -l
total 28268
-rw-r--r-- 1 fog  fog   4834144 Oct 10 11:31 2_6_35_7KS
-rw-r--r-- 1 root root 12872000 Jan 15 12:51 3_8_8core
drwxr-xr-x 2 fog  fog      4096 Oct 10 11:31 backup
-rw-r--r-- 1 fog  root 11227312 Nov 20  2012 bzImage
- Field 1: Permissions (owner, group, others)
- Field 2: links
- Field 3: owner
- Field 4: group

Change file ownership

chown -c {newowner} {filename}
Ex: chown -c fog 3_8_8core

Make a copy of a file (useful for backing-up config files before editing them)

cp demofile demofile.bak

Copy all files and directories in dev recursively to subdirectory bak:

cp -R dev bak

Rename a file from "foo" to "bar"

mv /tmp/foo /tmp/bar

Delete file

rm abc.txt

Delete directory (all files and folders recursively)

rm -rf /home/data/2000

Download file from internet (note: if unsuccessful, ensure your DNS is configured!)


Create .tar.gz archive of a directory (copies it into a .tar archive, then compresses with gzip)

tar cvzf archive_name.tar.gz dirname/

Extract .tar

tar -xvf file.tar

Extract .tar.gz

tar -xzvf file.tar.gz


Run a bash script



Ref: List all services

/sbin/chkconfig --list

Optional) check to see if service is already in chkconfig (otherwise you can't set it to start at boot)

/sbin/chkconfig --list sshd

Set service to start at boot (ex: "sshd")

/sbin/chkconfig sshd on

Turn nscd off in runlevels 2, 3, 4, and 5 (these are the default runlevels affected)

chkconfig nscd off

Turn nscd off in runlevels 3, 4, and 5

$ sudo chkconfig --level 345 nscd off

Turn apache on or off (CentOS)

/etc/init.d/httpd start
/etc/init.d/httpd stop

Stop iptables

# /etc/init.d/iptables save
# /etc/init.d/iptables stop

Start iptables

# /etc/init.d/iptables start



  • Gnome's built-in network mgmt utility, enabled by default with LiveCD install. It's recommended to use only one utility to manage network interfaces.
  • nmcli - command-line tool for controlling NetworkManager (included in NetworkManager package) (not great for configuration, better for monitoring)

Remove NetworkManager and configure CentOS networking by command-line

  • Uses RHEL-native "network" service, and simple networkscripts that are manually editable.



lftp is a pretty good FTP client for when you're working in the shell. It's native on at least some distros.

  • To enter lftp shell:
  • Exit shell:
  • To download file:
lftp -e 'set net:timeout 10; get yourfile.mp4; bye' -u user,password


curl is another common FTP client.

Webserver stuff

  • To find the username a webserver uses:
    • create a new file "whoami.php" containing the following:
      • <?php
      • echo shell_exec('whoami');
      •  ?>
    • Common default webserver usernames include "apache", "apache2", and "www-data".
  • If installing/configuring a script (eg. Wordpress, GLPI, etc) and you get warning about file permissions, try making the webserver user the owner of that directory:
    • "-R" does this recursively

Package Management

yum (RHEL/CentOS 5.x or above)

List all installed packages (one screen at a time)

yum list installed | more

Search for installed package "httpd"

yum list installed httpd

Search for available packages across all installed repos

yum search [keyword]

Display info about an installed package

yum info [packagename]

Location of yum's repository definitions (one file for each installed repo)

cd /etc/yum.repos.d

Location of yum's config file

/etc/yum.conf [3]

Group commands

Updating php above 5.3 in CentOS 6 using webtatic repo

yum repo management

To find all repositories currently "known" (enabled and disabled):

yum repolist all

Disable a repo:

edit the repo file, and set "enabled=0" for each entry in the file

Force yum to search or install from a specific repo: ("remi" in this case

yum --disablerepo="*" --enablerepo="remi" list available
yum --disablerepo="*" --enablerepo="remi" search php


Search for installed package "httpd"

rpm -qa | grep httpd*

Install a package

rpm -Uvh foo-1.0-1.i386.rpm

dpkg (debian)

List installed packages

dpkg -l
dpkg -l | more

List installed packages whose names start with "sqlite"

dpkg -l | grep sqlite*

aptitude (ubuntu)

To get just the packages which were expressly installed (not just installed as dependencies), you can run
aptitude search '~i!~M'


  • enable logging for this session:
SET GLOBAL general_log =1