skip to content
 

File Operations

[Note for windows users, a directory is what windows calls a folder.]

You need to imagine the unix file structure like an inverted tree. Every time you create a new directory it goes under/lower than the directory you create it from/inside.

Show me my files

Use ls to see your files
Use ls -l to see your files with details, e.g., who owns the file, who can read it,
Use ls -F to see the type of file, executables are marked with a *, directories with a /
Use ls -a to see all your files including the hidden ones, i.e., the ones starting with a "."
Use ls -R to see all your files in every directory in the directory you are currently in, ... every directory in every directory in every directory in the directory you are in... i.e., if you haven't already guessed, the -R is for recursive.

These options can be combined e.g., ls -l -F or ls -aRl

See also: The ls command

Changing Directories

To change directory use the cd command. E.g. I have a directory called .mozilla/firefox, I can see it with ls -a -F, in this directory my history and bookmarks files are kept. If I want to look at these without opening up Firefox, I can change directory to .mozilla/firefox with the cd command
cd .mozilla/firefox
To return to the directory I was in before the last command I can use cd -
To go up a level, e.g. from .mozilla/firefox to .mozilla, usually cd .. but may not always work because of symbolic links.
If you are lost in the file system cd will bring you back to your home directory - where you are when you login.
To visit another user's home directory (provided it's okay with them) use cd ~username

Where am I

If you don't know what directory you are in use pwd for print working directory to see where you are.

Reading text files

To read a text file use more filename or less filename, they both page though the file. Different people prefer one or the other. If you don't want to page through the file then use cat filename

Note: These only work on text files, and will mess up your terminal window if used on a binary file.

Suitable applications for viewing other types of file

Copying files

To copy a file - cp file newname
Or to copy a file to your home directory cp file ~/

Renaming files

To rename a file use mv file newname

To rename a number of files from one ending to another use the command rename. For example to rename a bunch of files from ending with htm to html do

        rename 's/\.htm$/.html/' *.htm

while to renumber files with names file1.gif, file2.gif to file0001.gif, file002.gif etc you could use

        rename 's/^file/file000/' file*.gif

 

  • The code beginning s/ is a Perl command to substitute a string for a regular expression (regexp). E.g. in the first command .html will be substituted for .htm.
  • In a regexp a dot stands for any single character - \. is how we specify a literal dot.
  • ^ means "beginning of filename" and $ means "end of filename". They are used to stop the command unexpectedly renaming a file with .htm or file in the middle of its name.

Editing text files

This is covered in a separate page on editors. Unix comes with a number of different editors, vi or emacs being the two most popular. A more intuitive one to get started with is pico (the editor used by pine)

Removing files

To remove or delete a file
rm filename

More details on the rm command

Removing directories

To remove or delete a directory, first of all make sure you have deleted all the files within it, including the hidden files, viewable with ls -a. When the only files left are . and .. then you can delete the directory. To delete it, change directory to the directory above it cd .. and then delete it with
rmdir directory-name

Alternatively, you can delete a directory and all its contents with
rm -r directory-name

Pushd and Popd

Another way of moving around directories is with pushd and popd. Imagine a stack, you can push directories on to it and pop them off. E.g. you are in /alt/ssetup/shellconfig looking at the default bash setup files when you decide you would like to see some timezone information. Instead of having to remember where you currently are in order to come back to it, use pushd to have the computer remember for you.
$ pushd /usr/share/zoneinfo
look at the files or maybe cd .. up to the next level etc, then to get back to where you were at the start
$ popd