Managing files and directories

On this page, we will look at the commands to manage files and directories in Unix:

  • Browsing, copying, transferring and deleting files and directories 
  • Data transfer between different Unix systems
  • Packing and unpacking files
  • Printing files

File and directory prompts

With Unix, files are usually handled via command line prompts. This is why it is very important for you to know the basics of using such prompts. If you are not sure how a certain prompt works, always check it with the man prompt before using it.


ls

This prompt shows the contents of the directory (ls, list).

Show the contents of the directory with the parameter ‘-al’ to show an extended list of data:
 % ls -al 
total 32
drwx------ 2 rkeskiva 8192 Aug 9 09:43 ./
drwx--x--x 11
rkeskiva 8192 Aug 14 13:54 ../
-rw------- 1
rkeskiva 3422 Aug 9 09:44 Thesis.rtf
-rw------- 1
rkeskiva 721 Aug 9 09:44 thesis.rtf

cat
This prompt outputs (cat, concatenate) the contents of a file (such as text) on the screen.

Print the contents of the file my_dear_alisa.txt on-screen:
 % cat my_dear_alisa.txt
Hi Alisa,

I wanted to write this letter so that you'd know
about my summer plans. I will be going to Cayman
Islands and I'd like you to join me. I think it
would be awesome to spend some time together



less

This prompt outputs the contents of a file on-screen, one screen at a time (handey especially when looking at a very long file). You can use the prompt more alongside this prompt.

Show the contents of the file my_dear_alisa_edited.txt one page at a time.
 % less my_dear_alisa_edited.txt

cd
This prompt is used for transferring from one directory to another (cd, change directory).

Go to the folder /tmp/alisaphotos (the slash / after the prompt shows that ther tmp folder is in the root and the alisaphotos folder is a sub-folder of temp:

 % cd /tmp/alisaphotos

When you have opened the folder alisaphotos mentioned above, you see that there is yet another sub-folder called originalphotos in this folder. You can open this sub-folder with the following prompt, without using /:

 % cd originalphotos

cp
With this prompt, you can copy files from one place to another (cp, copy).

Copy the image raymond_alisa_original.jpg from its original directory to the sub-directory /tmp/rayray:
 % cp raymond_alisa_original.jpg /tmp/rayray

 

mkdir
With this prompt, you can create new directories (mkdir, make directory).

Create a directory called alisaphotos in the home directory of user rkeskiva:
 % mkdir /home/rkeskiva/alisaphotos

mv
With this prompt, you can move files from one place to another (mv, move). You can also rename files with this prompt.

Move the file raymond_alisa_original.jpg from the directory /tmp/rayray to the directory /home/rkeskiva/alisa-nimiseen:
 % mv /tmp/rayray/raymond_alisa_original.jpg /home/rkeskiva/alisa


rm
With this prompt, you can delete files.

Delete the file called cookbook_oldversion.txt:
 % rm cookbook_oldversion.txt 

rmdir
With this prompt, you can delete directories.

Delete the directory rayray from the tmp directory:
 % rmdir /tmp/rayray

 

pwd
This prompt outputs your current working directory on-screen (pwd, print working directory).

 % pwd 
/home/rkeskiva/backup

 

zip
With this prompt, you can pack files or directories. Gived the prompt the name of the package to be created and then the name of the file or directory.

Pack the directory alisaphotos into a package called alisaphotos.zip. Note that if you use ‘-r’ as parameter, the packing prompt handles the folder alisaphotos recursively – i.e. including all possible sub-directories: 
 % zip alisaphotos.zip -r /home/rkeskiva/alisaphotos/
adding: /home/rkeskiva/backup/ (stored 0%)
adding:
/home/rkeskiva/backup/thesis.rtf (deflated 91%)
adding: /home/rkeskiva/backup/Thesis.rtf (deflated 98%)

unzip
This prompt unpacks zipped files. Give the prompt the name of the package to be unzipped to unpack it in its original size.

Unpack the file cookbook.zip. Note that these files will not be unzipped into their original directory, but the unzip program will create a directory called  ‘home/rkeskiva/backup’ in the current working directory.
 % unzip cookbook.zip
Archive: koe.zip
creating:
home/rkeskiva/backup/
inflating:
home/rkeskiva/backup/thesis.rtf
inflating:
home/rkeskiva/osalo/backup/Thesis.rt

So-called tar packages are frequently used in Unix. Like zip packages, they can contain other files and directories. Read more about how to use this prompt by typing the prompt ‘man tar.’


grep

This prompt is used for searching for a character string. This prompt is often used with the sign | (pipe) som that the program pipelines the output as the input for the following program.

Look for occurences of the word ‘Bouillabaisse’ in the file cookbook.txt. The prompt grep will only give the lines where the word ‘Bouillabaisse’ occurs in the file cookbook.txt (1 line in the example): 
 % cat cookbook.txt | grep Bouillabaisse
Bouillabaisse - the Best Bouillabaisse-recipe ever!

 

Transferring files between Unix systems

With the prompts sftp and scp, you can copy files from one Unix system to another over a secure connection.


sftp

With this prompt, you can create a data-transfer connection from one server to another. When this prompt is used, the connection will be secure. Please note that you cannot access the UH Unix system with an unprotected FTP program at all, but you must always use a protected FTP connection.

Use an sftp program to open a data-transfer connection to a server called klaava:
 % sftp klaava
rkeskiva@klaava's password:

When using the prompt described above, the data-transfer connection will be made from one Unix server to another. You can also transfer files between other operating systems; read more about file transfer between Windows and unixi with the program WinSCP.

scp
You can use this prompt to copy files to another server in a similar way as with the sftp program.

Copy the file called alisa.txt to the server sirppi, into user rkeskiva’s home directory and folder love:
 % scp alisa.txt rkeskiva@sirppi:love/
rkeskiva@sirppi's password:
alisa.txt | 203B | 203B/s | TOC: 00:00:01 | 100%

Copy the file tiramisu.txt in the directory recipes in rkeskiva’s home directory on the server sirppi into the current working directory (the current directory is marked in the prompt with a dot):
 % scp rkeskiva@sirppi:recipes/tiramisu.txt .
rkeskiva@sirppi's password:
tiramisu.txt | 3.3kB | 3.3kB/s | TOC: 00:00:01 | 100%

Prompts for printing

There are different ways to print from the command line in Unix. The following prompts will execute the most common printing tasks:

lpr
This prompt prints the file you name.

lpq
This prompt will show you the printer queue.

lprm
You can delete your job from the printer queue with this prompt.

mpage
The printing program for text and e.g. postscript or ps files. You can use different parameters to specify e.g. on how many sheets of paper you want to print your job. Read more about how to use this prompt by typing ‘man mpage’ on the command line.

Read more about Unix prompts:  http://www.cs.helsinki.fi/u/jzhaavis/tyovaline/unix.html