Saturday, 8 February 2014

Linux Basic Commands – File System Navigation

cd – Change directory

pwd – Print name of current working directory

ls – List directory contents

file – Print a brief description of the file’s contents.

CD Commands

cd command, is the main command you use to navigate around the file system.  While moving, the pwd command, will tell you where you are now in the current file system. ls command will list the files and directory contents in your current directory.

Root Directory

The Root Directory or “/” is the start of the file system directory, or parent of all directories.

Absolute Pathnames

An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed.  For example,

$ cd /etc/network    (changes your current directory to root --> etc --> network directory)
$ pwd
/etc/network

Relative Pathnames

The shell uses special symbols to represent relative positions in the file system tree.

  • “.” symbol refers to the working directory
  • “..” symbol refers to the parent directory of the current working directory.
  1. “cd ..” means move up to parent directory
  2. “cd ./bin” means from current directory move down to a directory name “bin”
  3. “cd bin” and point 2 does the same thing.

In general if you do not specify a pathname to something, the working directory will be assumed.

cd Shortcuts

  • cd
    Changes the working directory to your home directory
  • cd -
    Changes the working directory to the previous working directory.
  • cd ~user_name 
    Changes the working directory to the home directory of user-name.

Facts about Filenames

  1. Filenames that begin with a period character are hidden. ls command will not list them unless you use ls -a.
  2. Filenames are case sensitive.
  3. There is no concept of “file extension”.
  4. You can, but try not to use punctuation characters in filenames.  Limit the punctuation characters in the filename  you create to “.” period, “-“ dash and “_” underscore. 
  5. You can, but try not to use spaces between filename. Try to use “_” (underscore) to replace your spaces between filename.

The ls Command

  • ls
    List current directory.  List filename only.
  • ls /usr
    List directory under /usr directory
  • ls ~
    List home directory
  • ls -a
    List all files, including hidden files.
  • ls -l or ll
    List current directory with long format.  File permission rights, owner, group, size, date time last modified, filename, links.

The file Command

As there is no concept of “file extension”, you can use the “file” command to print out a brief description of the file’s contents.

$ file /bin/ls
ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf31e99218b4d7034cf8257055686bca22f5a3c01, stripped

$file * (list all file type in the current directory)

Base Directories

  • /
    The root directory.  Where everything begins.
  • /boot
    Contains the Linux kernel (vmlinuz), initial RAM disk image (initrd.img) and boot loader (grub).
  • /bin
    Contain common binaries programs. Example, bash, cat, chmod, chgrup, chown, date, grep, kill, less, login, ls, mkdir, rm, rmdir, ping, sed, su, tar.
  • /sbin
    This directory contains system binaries. These are programs that perform system tasks that are generally
    reserved for the superuser. Example, fsck, ifconfig, ifdown, ifup, mkfs, reboot, reload, route, runlevel, start, stop, shutdown.
  • /lib
    Contains shared library files used by the core system programs. These are similar to DLLs in Windows.
  • /usr
    The /usr directory tree is likely the largest one on a Linux system. It contains all the programs and support files used
    by regular users.
      • /usr/bin, contains the executable programs installed by your Linux distribution.
      • /usr/sbin, contains more system administration programs
      • /usr/lib, contains shared libraries for the programs in /usr/bin
  • /opt
    The /opt directory is used to install “optional” software. This is mainly used to hold commercial software products
    that may be installed on your system.  Most of the software will be placed here after system setup.
  • /etc
    The /etc directory contains all of the system-wide configuration files. It also contains a collection of shell
    scripts which start each of the system services at boot time. Everything in this directory should be readable text.
      • /etc/crontab, a file that defines when automated jobs will run
      • /etc/fstab, a table of storage devices and their associated mount points.
      • /etc/passwd, a list of the user accounts
      • /etc/hostname, define of system name
      • /etc/hosts, IP to name hosts file
      • /etc/profile, bash system-wide profile for users
  • /dev
    This is a special directory which contains device nodes. “Everything is a file” also applies to devices. Here is where
    the kernel maintains a list of all the devices it understands.
  • /media
    On modern Linux systems the /media directory will contain the mount points for removable media such USB
    drives, CD-ROMs, etc. that are mounted automatically at insertion.
  • /mnt
    On older Linux systems, the /mnt directory contains mount points for removable devices that have been mounted
    manually.
  • /proc
    The /proc directory is special. It's not a real file system in the sense of files stored on your hard drive. Rather, it is a
    virtual file system maintained by the Linux kernel. The “files” it contains are peepholes into the kernel itself. The
    files are readable and will give you a picture of how the kernel sees your computer.
  • /lost+found
    Each formatted partition or device using a Linux file system, such as ext3, will have this directory. It is used in the case of a partial recovery from a file system corruption event. Unless something really bad has happened to your system, this directory will remain empty.
  • /home
    In normal configurations, each user is given a directory in /home. Ordinary users can only write files in their home
    directories. This limitation protects the system from errant user activity.
  • /root
    This is the home directory for the root account.
  • /tmp
    The /tmp directory is intended for storage of temporary, transient files created by various programs. Some
    configurations cause this directory to be emptied each time the system is rebooted.
  • /var
    The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, etc. are located here.
      • /var/log, contains log files, records of various system activity. These are very important and should be monitored from time to time.

No comments:

Post a Comment