unixBasic console commands

pwd - print working directory

$> pwd
/home/myUserHome
$> cd ..
$> pwd
/home

will print the current path to the console.

pushd/popd (store current dir on stack and go to dest / pop prev dir and go to it)

$ pwd
/home/bob/somedir1/somedir2/somedir3

$ pushd /home/bob/otherdir1/otherdir2
/home/bob/otherdir1/otherdir2 /home/bob/somedir1/somedir2/somedir3

$ popd
/home/bob/somedir1/somedir2/somedir3

$ pushd /usr
/usr /home/bob/somedir1/somedir2/somedir3

$ pushd /var
/var /usr /home/bob/somedir1/somedir2/somedir3

$ popd
/usr /home/bob/somedir1/somedir2/somedir3

$ pwd
/usr

$ popd
/home/bob/somedir1/somedir2/somedir3

$ pwd
/home/bob/somedir1/somedir2/somedir3

file manipulation commands

List of commands that will be introduced here:

ls     #view contents of a directory
touch  #create new file
mkdir  #create new directory
cp     #copy contents of one file to another
mv     #move file from one location to another
rm     #delete a file or directory

ls examples

[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     test.cpp

shows the current directory

[email protected]_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp        encryption.cpp  pool_game.cpp

shows the directory "c++ projects". Space characters in file names are typed as "\ ".

touch example

[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     test.cpp
[email protected]_computer:~/Desktop$ touch ruby_test.rb
[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     ruby_test.rb    test.cpp

mkdir example

[email protected]_computer:~/Desktop$ mkdir ruby
[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     ruby    ruby_test.rb    test.cpp
[email protected]_computer:~/Desktop$ cd ruby
[email protected]_computer:~/Desktop/ruby$ ls
<nothing>
[email protected]_computer:~/Desktop/ruby

It doesn't actually print <nothing>. It's just how I'm representing that it doesn't output anything

cp examples

[email protected]_computer:~/Desktop/ruby$ cd ..
[email protected]_computer:~/Desktop$ cp test.cpp c++_test.cpp
[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby_test.rb
test.cpp

This is when the last arg to cp, in this case "c++_test.cpp" is not an existing directory. cp will create a file called "c++_test.cpp", with contents identical to that of "test.cpp". If c++_test.cpp already existed, cp would have deleted what was previously there before copying the contents of "test.cpp" over.

[email protected]_comptuer:~/Desktop$ ls ruby
<nothing>
[email protected]_computer:~/Desktop$ cp ruby_test.rb ruby
[email protected]_computer:~/Desktop$ ls ruby
ruby_test.rb

This is what happens when the last arg to cp, in this case "ruby", is a directory. cp creates a file with the same name as "ruby_test.rb", but in the directory "ruby".

mv examples

[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby_test.rb
test.cpp
[email protected]_computer:~/Desktop$ mv ruby_test.rb ruby\ test.rb
[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby test.rb
test.cpp

This is what happens when the last arg to mv, in this case "ruby test.rb", is not an existing directory. The file "ruby_test.rb" has been renamed to "ruby test.rb". If "ruby test.rb" already existed, it would have been overwritten Note, again, that spaces are preceded by a ''.

[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby test.rb
test.cpp
[email protected]_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp        encryption.cpp  pool_game.cpp
[email protected]_computer:~/Desktop$ mv test.cpp c++\ projects
[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby test.rb
[email protected]_computer:~/Desktop$ ls c++\ projects
DNA_analysis.cpp        encryption.cpp  pool_game.cpp   test.cpp

This is what happens when mv is a directory that already existed. The file "test.cpp" gets moved to the directory "c++ projects".

rm examples

[email protected]_computer:~/Desktop$ ls
c++ projects    c++_test.cpp    Research Paper.docx     ruby    ruby test.rb
[email protected]_computer:~/Desktop$ rm c++_test.cpp
[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     ruby    ruby test.rb

c++_test.cpp has been deleted

[email protected]_computer:~/Desktop$ rm c++\ projects
rm: cannot remove 'c++ projects': Is a directory
[email protected]_computer:~/Desktop$ ls
c++ projects    Research Paper.docx     ruby    ruby test.rb

rm has an extra requirement to delete directories

[email protected]_computer:~/Desktop$ rm -rf c++\ projects
[email protected]_computer:~/Desktop$ ls
Research Paper.docx     ruby    ruby test.rb

-rf must be added to delete a directory.

To learn more about ls, type the command ls --help. For touch, type touch --help. Likewise with all 6 commands mentioned here. This prints out a detailed explanation of use without creating or deleting anything.

cd command, directories explained

[email protected]:~$

The symbol ~ after the who-cares: is the current directory. ~ actually means the person's home directory. In this case, that's /home/michael.

[email protected]:~$ cd Downloads
[email protected]:~/Downloads$

Looks for Downloads in the current directory, then makes that the current directory.

[email protected]:~/Downlaods$ cd /var
[email protected]:/var$

Since this directory started with a /, that means look in the root directory for the directory var. For those coming from windows, the root directory is the equivalent to C:\. Directories starting with / are called "absolute directories" and directories that don't are called "relative directories"

[email protected]:/var cd lib/dbus
[email protected]:/var/lib/dbus$

The / in the middle means do cd lib and once that's done cd dbus in one command.

[email protected]:/var/lib/dbus$ cd .
[email protected]:/var/lib/dbus$

. actually means "the current directory". The command cd . is basically useless, but . is useful for other things.

[email protected]:/var/lib/dbus$ cd ..
[email protected]:/var/lib$

.. actually means "the parent of the current directory". As such, cd .. means "navigate one directory up".

[email protected]:/var/lib$ cd ../log/apt
[email protected]:/var/log/apt$

. and .. can also be part of the / chain. Also, there's no limit to how long it can be.

[email protected]:/var/log/apt$ cd /dev/bus
[email protected]:/dev/bus$

The / chain can even exist when the directory starts at root.

[email protected]:/dev/bus$ cd /
[email protected]:/$

cd / takes you to the root directory. I wonder what happens if you type cd .. here... (don't worry. It's safe)

[email protected]:/$ cd home
[email protected]:/home$ cd michael
[email protected]:~$

Every user has a directory for their stuff inside the home directory. If current directory is under the home directory, that part of the name, in this case /home/michael, it's replaced with ~.

[email protected]:~$ cd sys
[email protected]:/sys$ cd ~/Desktop
[email protected]:~/Desktop$ cd ~/..
[email protected]:/home$

~ can also be part of the / chain. It can even be in the same chain as ... If the directory starts with ~, it's an absolute directory just like if it starts with /.

Last thing to try: type cd with no directory after.

which

To determine where on your system an executable in your path exists, use the which command:

$ which python
$

If there is no response, that executable does not exist in your path. The system will simply return you a new prompt without an error message. If the executable does exist on your path, it will show the directory where it actually exists:

$ which ls
/bin/ls

This can be helpful in determining why behavior does not match expectation by ensuring you're executing the version of the executable that you think you are. For instance, if you have both Python 2 and Python 3 installed, they both might be executed by typing python in the terminal - but the executable actually being run may be different than expected. As shown above this command will work for any standard unix command, which are all backed by individual executables.

Basic Unix commands

$pwd

Displays the present working directory.

$who

Displays all the users logged in.

$who am i

Shows the username of the current user.

$date

Displays the current system date

$which <command>

Shows the path of the specified command. For example "$which pwd" will shows the path of 'pwd' command.

$file <file_name>

Shows the type of the specified file(regular file, directory or other files)

$cal

Displays the calender of the current month.

$bc

Shows the mathematical calculation between two integers of floats. For example "$bc 2+3" will returns the arithmetic sum of 3 and 5.

$ls

Lists the contents of the directory.

  • $ls -l : lists in long format.
  • $ls -c : Multi column output.
  • $ls -f : Lists the type of file.
  • $ls -r : Recursive listing of all subdirectories encountered.
  • $ls -a : Displays all files including hidden files.
  • $ls -i : Lists all files along with its I-Node number.
$grep [options] <pattern> <input_file_names> 

Prints lines that contain a match for a pattern.

  • $grep -i : Perform case insensitive matching
  • $grep -v : Prints all lines that don’t contain the regex
  • $grep -r : Recursively search subdirectories listed and prints file names with occurrence of the pattern
  • $grep -I : Exclude binary files