Home Scripting Introduction Basics   Redirect Cut/Paste Quoting Regular Expressions Sed Awk Scripts Books

Basics


Contents

This chapter goes through some basic commands on Unix/Linux and how to edit files. Some notes to help navigate the Unix system in an easier manner.
On a Unix system if we are in a folder and there are other files and files in that folder then we do not need to type in the full name of the file. We can type in the first few characters and then hit the "Tab" key and if there is no ambiguity then the system will type the name for us.
We can use the up and down arrow keys to go through the history of commands we have typed in a session.

file command

This section will examine the file structure and some of the most commonly used commands when dealing with files.
When we first login to the system we have a command prompt. As discussed in the "Introduction" chapter Unix does not have the letters "C:" , "D:" for the file system. Rather we start from the root "/" .
After logging in to hills we are presented with a prompt. We can check our
current directory with the pwd command.

pwd

[amittal@hills ~]$ pwd
/users/amittal
[amittal@hills ~]$

The "pwd" stands for "present working directory"
and shows the folder that we are in right now.
It will probably be different for you because
you are a student. It might look like:


[amittal@hills students]$ pwd
/students

ls

To list the files we can use the "ls" command.
[amittal@hills ~]$ ls
1.txt              doga                                       MongoDB
6_5_2021           editors                                    nano.save
accounts.csv       f1.sh                                      nano.save.1
admin              f2.sh                                      Networking

A command will usually come with some options. The
"ls" command has options such as "-l" , "-t" and "-r".
We can give multiple options by using a single hyphen
followed by the options.


[amittal@hills 2]$ ls -l
total 0
-rw------- 1 amittal csdept 0 Dec 22 19:14 2.txt

The "-l" give the long listing with the permissions,
owner of the file, the size of the file, the date when
the file was created.
The "ls -lt" will do the long listing and also sort the files
by their created/modified time.

[amittal@hills 2]$ ls -lt
total 0
-rw------- 1 amittal csdept 0 Dec 22 19:49 3.txt
-rw------- 1 amittal csdept 0 Dec 22 19:14 2.txt

The file "3.txt" is listed first as it's time of "19:49"
is newer. We can reverse the order using the "r" option.


[amittal@hills 2]$ ls -ltr
total 0
-rw------- 1 amittal csdept 0 Dec 22 19:14 2.txt
-rw------- 1 amittal csdept 0 Dec 22 19:49 3.txt

Now the latest file is shown at the bottom.

man
It is difficult to remember all the options associated
with all the commands. Luckily we have the "man" ( manual )
command that gives us the information about the command.

[amittal@hills 2]$ man ls

We can hit "q" to come out of the "man" output screen
and the scroll bar to go down a page.


touch
The "touch" command updates the modified timestamp of an existing
file and will create a new empty file if the file does not
exist. This is a handy command to create empty files.


mkdir

To create a folder we use the "mkdir" command. Let's try
creating the "cs160a" . We can use this folder to work on
our homeworks. We can organize our homeworks by placing them
in here.


[amittal@hills ~]$ mkdir cs160a

We can go to this folder using the "cd" command.

cd
The "cd" command is used to change to a folder. If we
use "cd" without any arguments then we change to
our home folder ( the folder we land up in when we login )

[amittal@hills ~]$ cd ..
The ".." means move 1 folder up whereas "." means the current
folder.


[amittal@hills ~]$ cd cs160a
[amittal@hills cs160a]$

The "cd" command means change director. Now if we do a "pwd"
we will see that the full path of where we are:

[amittal@hills cs160a]$ pwd
/users/amittal/cs160a

We have the concept of full paths and relative paths. The full
path will start from the root "/" symbol while the relative path
is a partial path that will use the current folder. Say we are
in the folder

"/users/amittal"
and we want to create a folder called "test1" in the "amittal" folder.
We can either do :

mkdir /users/amittal/test1

However since we are already in the "users/amittal" folder
we can just do:

mkdir test1

and the folder "test1" is created in the "/users/amittal/"
folder.



rm

The "rm" command removes files.

[amittal@hills 1]$ touch 1.txt
[amittal@hills 1]$ rm 1.txt



rmdir
The "rmdir" command removes a directory.


[amittal@hills cs160a]$ mkdir 1
[amittal@hills cs160a]$ rmdir 1

In the above the folder "1" was empty. We need
to write the command differently for non-empty
directories.
For the below we have a non-empty directory called "2"
which has a file called "2.txt" in it.

[amittal@hills cs160a]$ rm -r 2
[amittal@hills cs160a]$

cp

cp
The command "cp" copies a file. It's syntax is:

cp sourcefile destination
The destination can be another file in which case the sourcefile
gets copied to another file with the name of the destination or the
destination cam be a folder in which case the sourcefile gets copied
to the destination folder with the same name as sourcefile.

Create two folders named "first" and "second" .
Create a file called "1.txt" in the folder "first" and then
copy it over to a second file called "2.txt" in the same folder.

We have created a folder called "copy" to
tese the commands. The structure is as follows:

~/cs160/copy/
             first
             second

$ mkdir first
$ mkdir second
$ cd first
$ touch 1.txt
$ cp 1.txt 2.txt
$ ls
1.txt  2.txt

Now we are going to copy over these 2 files to the
folder named "second" .
$ pwd
~/cs160/copy/first
$ cp * ../second
$ cd ..
$ cd second
$ ls
1.txt  2.txt

The "*" means all the files. We could have also used the
command:

$ cp 1.txt 2.txt ../second

We can also traverse to the "second" folder with
just 1 command:

$ cd ../second


cp can copy all the contents of a folder to another folder .

cp -r folder1  folder2

This will create a "folder2" with all the contents from "folder1"


$ cp -r first third
$ ls
first  second  third

We can list the contents of the third folder to make sure
the files got copied properly.

$ ls third
1.txt  2.txt


Editing



If we are on a Windows system and we installed the WinScp utlility then we can edit the files on the hills server directly. However it is important to be able to edit files using the console. There are 2 modes. One is the command mode and the other edit mode. We use the command mode to save, delete copy paste and the edit mode to type in characters.
Create a folder called "edit" and then the command.

$ vi 1.txt

We use the "i" key to enter the edit mode. Type in a few lines and then hit the "Esc" key. After that hit the ":" key and type in "wq" ( w to write and q to quit ) . A useful resource is the vi cheat sheet at:

vi cheat sheet

general commands

  • Now that we can create files with some contents here are some
    more commands.
    cat
    The "cat" command will list the contents of a file. We can
    use it to list the contents of multiple files also.
    
    $ cat 1.txt 2.txt
    
    date
    Prints the current date and time.
    [amittal@hills edit]$ date
    Sat Dec 23 19:46:21 PST 2023
    
    clear
    The "clear" command clears the console and brings the
    cursor to the top of the console.
    
    echo
    The "echo" command can print a string on the console.
    
    [amittal@hills edit]$ echo "This is a unix class."
    This is a unix class.
    
    It can also print values of variables ( we shall study
    the variables in more details later on ) .
    
    
    [amittal@hills edit]$ echo $PATH
    /usr/local/bin:/usr/bin:/usr/local/sbin:
    /usr/sbin:/sbin:/users/amittal/.local/bin:
    /users/amittal/bin
    
    tar
    The "tar" command can be used to compress and extract files. To
    compress the folder "mymath" to a file called "mymath.gz"
    
    tar -czvf  mymath.gz mymath
    To extract the compressed file use:
    
    The "c" option means create a tar file.
    The "z" option means filter the archive through gzip.
     Use "gzip" to compress.
    The "v" option means verbose.
    The "f" option means specufy the archive file.
    
    tar -xvf mymath.gz
    This will create a folder "mymath" in the current directory and
    extract all the files there.
    The "x" option means extract.
    
    who
    The "who" command shows the list of users who are logged
    in the system.
    The related command "whoami" shows the current user who is
    logged in.
    
    [amittal@hills ~]$ whoami
    amittal
    
    wc
    [amittal@hills 1]$ cat 1.txt
    This is a unix class.
    [amittal@hills 1]$ wc 1.txt
     1  5 22 1.txt
    
    The "wc" lists the lines, words and characters in the
    file.
    
    find
    [amittal@hills cs160a]$ find . -name "1.txt"
    ./hw1/first/1.txt
    ./hw1/first/second/1.txt
    ./hw1/second/1.txt
    ./edit/1.txt
    ./1/1.txt
    
    The find command can be used to search for files.
    In the example we are stating that the search start
    from the current folder ( "." ) and that the name
    of file should be "1.txt" .
    
    grep
    The "grep" command searches for a string. If we give the command
    and the string then "grep" expects the input from the console.
    If there is a match then the line is printed out again.
    
    $ grep cat
    The cat is sleeping.
    The cat is sleeping.
    The dog eats food.
    
    In the below the command "grep" looks in the
    file "3.txt" and prints the lines that
    contain the word "cat" .
    
    $ cat 3.txt
    The cat is sleeping.
    The dog eats food.
    
    $ grep "cat" 3.txt
    The cat is sleeping.
    
    
    
    
    
    
    wc who permissions cat clear echo date Find hw cp -r also .