Results 1 to 1 of 1
Thread: Basic *nix Commands Part 1
03-18-2003, 09:48 PM #1
- Join Date
- Feb 2003
Many shell scripts involve copying, moving, or removing files. All of these tasks can easily be handled from a standard shell.
The command used to view a list of files in the current directory is "ls". Typing "ls" at a command prompt will result in a list of all files in the current directory being displayed.
Copying files can be done with the cp command. The cp command takes as arguments a list of files to copy (the source) followed by the destination (where the files are to be copied to). Source lists containing more than one file must be followed by a destination directory. When copying a single file, the destination argument can be a directory to copy the file into, or the name of the new file to create. Consider the command "cp hello.txt world.txt goodbye.txt /tmp". This command will copy the three text files specified (assuming they exist and are in the current directory) to the standard Unix temporary directory /tmp. After the command finishes the /tmp directory will contain copies of the three text files. Now consider the command "cp hello.txt none.txt". After the command finishes, the file "none.txt" in the current directory will exist and will be an exact duplicate of the hello.txt file.
Moving or renaming files from the Unix Shell is done with the mv command. Mv behaves a lot like copy. When moving multiple files the last argument on the command line specifies the name of the destination directory. When moving a single file the last (second) command line argument can either be the directory into which the file will be moved or the name the file will be given. For example "mv hello.txt world.txt goodbye.txt /tmp" will move the three text files into the /tmp directory. After the command executes the /tmp directory will contain the three files and the current directory will no longer contain the files. The command "mv hello.txt none.txt" will rename the file hello.txt to none.txt. Nothing else besides the name of the file will change.
Deleting a file from the Unix shell is done with the "rm" command. To delete a file, type rm followed by the file name to delete. If you want to delete multiple files then type the names of all the files to delete. For example to delete the three text files used in the copying example type "rm hello.txt world.txt goodbye.txt". It is important to remember that Unix does not normally have an undelete function. Once a file is deleted it is gone forever. Directories can also be deleted with the rm command. To delete a directory(and everything in that directory) use the –r (for recursive) option. If you wish to delete the directory /tmp/demo and everything in it, the command "rm -r /tmp/demo" will work.
By Steve Singer