Tuesday, 17 February 2015

Helpful command-line tips for Linux users part 2: working with directories - cd

list of all commands
explanation of the convention:
[] : values in square brackets are optional
| : pipe means OR

cd BASIC USAGE 
cd [-L|-P] [directory] - change your current directory to [directory]
for example this will take you to directory var:
cd /var

USEFUL USES

go to your home directory:
cd

To go back to the previous working directory:
cd -

Go one directory up in the hierarchy:
cd ..

Script the above to do more by adding this to .bashrc:
function cd () {
  local -ri n=${#*};
  if [ $n -eq 0 -o -d "${!n}" -o "${!n}" == "-" ]; then
    builtin cd "$@";
  else
    local e="s:\.\.\.:../..:g";
    builtin cd "${@:1:$n-1}" $(sed -e$e -e$e -e$e <<< "${!n}");
  fi
}
Now cd.. takes you to parent dir, cd ... takes you two up, cd .... 3 up and so on

No comments:

Post a Comment

Real Time Web Analytics