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.
This prompt shows the contents of the directory (ls, list).
% ls -al
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
This prompt outputs (cat, concatenate) the contents of a file (such as text) on the screen.
% cat my_dear_alisa.txt
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
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.
% less my_dear_alisa_edited.txt
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
With this prompt, you can copy files from one place to another (cp, copy).
% cp raymond_alisa_original.jpg /tmp/rayray
With this prompt, you can create new directories (mkdir, make directory).
% mkdir /home/rkeskiva/alisaphotos
With this prompt, you can move files from one place to another (mv, move). You can also rename files with this prompt.
% mv /tmp/rayray/raymond_alisa_original.jpg /home/rkeskiva/alisa
With this prompt, you can delete files.
% rm cookbook_oldversion.txt
With this prompt, you can delete directories.
% rmdir /tmp/rayray
This prompt outputs your current working directory on-screen (pwd, print working directory).
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.
% 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%)
This prompt unpacks zipped files. Give the prompt the name of the package to be unzipped to unpack it in its original size.
% unzip cookbook.zip
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.’
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.
% 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.
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.
% sftp klaava
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.
You can use this prompt to copy files to another server in a similar way as with the sftp program.
% scp alisa.txt rkeskiva@sirppi:love/
alisa.txt | 203B | 203B/s | TOC: 00:00:01 | 100%
% scp rkeskiva@sirppi:recipes/tiramisu.txt .
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:
This prompt prints the file you name.
This prompt will show you the printer queue.
You can delete your job from the printer queue with this prompt.
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