File Operations
- Listing Files
- Changing Directories
- Where am i
- Reading files
- Copying files
- Renaming files
- Editing files
- Removing files
- Removing directories
- Pushd and Popd
[ Note for windows users, a directory is what windows calls a folder. ]
You need to imagine the unix file structure like an inverted tree. Everytime you create a new directory it goes under/lower then the directory you create it from.
Show me my files
Use ls to see your files
Use ls -l to see your files with details, eg 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, ie 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... ie, if you haven't already guessed, the -R is for recursive.
These options can be combined eg ls -l -F or ls -aRl
Changing Directories
To change directory use the cd command eg I have a directory called .netscape, 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 netscape, I can change directory to .netscape with the cd command
cd .netscape
To return to the directory I was in before the last command I can use cd -
To go back up a level, usually the same as the above, but may not be cd .. due to 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 its 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 files
To read a file use more filename or less filename, they both page though the file. Different people perfer one or the other. If you don't want to page through the file then use cat filename
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 htm html *.htm
while to renumber files with names file1.gif, file2.gif to file0001.gif, file002.gif etc you could use
rename file file000 file?.gif
Editing files
This is now covered in a seperate 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
Removing files
To remove or delete a file
rm filename
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
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. eg you are in /usr/local/etc/defaults 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.
$ pushd /etc/zoneinfo/sources
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