Revision 1 as of 2007-11-05 18:47:52

Clear message
Edit History Actions Discussion

Howto/FileManagement

File Management

This is work in progress, not complete. Please help and add more stuff!

This is a howto explaining how to manage files on a UNIX system. This is very important to get the most out of your shell account.

Basics

The very basics of file management is how to list, create, copy, move and delete files and directories.

To create a file, you can open up a text editor, write some text, and then save it. To do this with nano, type:

nano myfile.txt

This will open up the text editor nano. Write some text and press ctrl-x to exit, nano will then ask if you want to save the file. You can also use ctrl-o to just save the file, without exiting nano.

You can also use this command to just create a new empty file:

touch newfile

To create a new directory, use this command:

mkdir mydir

To see your newly created files and directories, use the command ls:

ls

To copy a file, we use the command cp. You have to specify what file(s) to copy, and then their destination. Like this:

cp myfile.txt mydir/

This will copy your file myfile.txt into the directory mydir. To copy a whole directory tree, specify the flag -r like this:

cp -r mydir/ mydir-bak

Note: You should try to learn and use "tab-completion" when you move around files etc. You do this by writing the first couple of letters in a name, and then press Tab on your keyboard. The file/directory name should then be automatically completed. Try it out!

To move in to your newly created directory, use the command cd like this:

cd mydir/

You can move back by using cd .. (moves one level up in the directory tree), or just cd with no arguments to move back to your home directory.

To display what directory you're currently in (working directory), use this command:

pwd

To move a file or directory, use the command mv. Like this:

mv myfile.txt ../

Which would move myfile into the parent directory (one level up in the directory tree.)

You can also use the mv command to rename files, like this:

mv myfile.txt renamedfile.txt

Lastly, to delete files, use the command rm, like this:

rm renamedfile.txt

To remove a whole directory, you have to use the flag -r.

Understanding and listing permissions

In UNIX, all files belongs to a user and a group.

For example, your files on the shell will be owned by yourself, and belong to the group shelluser by default.

You can set permissions for your files based on this owner and group. There are three possible permissions, read, write and execute. And you can set these permissions on the owner, the group, or "others". For example, the owner (you) can read and write, while the group can only read, and "others" (those who are neither the owner or in the group) can't do anything with the file.

To view the permissions on a file, you can use the command ls with the flag -l to get a detailed output of files in a directory:

{{{ls -l

-rw-r--r-- 1 independence shelluser 0 2007-11-05 19:32 myfile.txt}}}

The first 10 characters signify permissions on this file. You can also see the owner and group of the file, when it was last modified, and the actual filename.

The 10 characters are divided into 4 groups. The first group is only one character, and this position displays if this item is a directory, in this case it's not so there's just a - displayed (instead of a d.)

Then there are three groups of three caracters. The first group displays what permissions the owner has got, the second group the group permissions, and the last group permissions for others.

In this case, the first three letter group is "rw-". This means that the owner has read and write permissons on the file, but not execute permissions (which would have been displayed by an x.)

The second and third group are both "r--" which means that the group and others both have only read permissions on the file. This is the default of newly created files.

Changing permissions

To change the owner of a file, you can use the command chown. Like this:

chown independence myfile.txt

This will change the owner of myfile.txt to independence. You can also specify a new group like this:

chown independence:othergroup myfile.txt

You can only change ownership of files that you own yourself, not others' files (duh.)

To change the permissions of a file, we use the command chmod.

We can specify that the group should get write permissions for example:

chmod g+w myfile.txt

The syntax here is first who you want to change the permission for:

  • u (user)
  • g (group)
  • o (others)
  • a (all, ie user+group+others)

Then you specify either + or - to signify that you want to add or remove a permission.

Lastly, you specify what permission you want to add or remove:

  • r (read)
  • w (write)
  • x (execute)

Some more examples:

Give the owner user execute permissions on myfile: chmod u+x myfile.txt

Remove write permissions for others: chmod o-w myfile.txt

Play around a bit yourself, and view the result with:

ls -l yourfile.txt

Disk usage

ls -lh

quota

du -hs

Compressing/achives

tar

gzip

bzip

ACLs

getfacl

setfacl