Manipulating Files
This page is intended to help the Linux newbie come up to speed on core file handling commands including file permissions. Brought to you by Mark Rais, our senior editor. To read comands for Directories please click here.

Included in this section are the commands needed to copy, delete, move, and rename files. Security and permissions are also reviewed below in the chmod command.

Manipulating Files - Linux Commands

Command

Summary Use
chmod The chmod command allows you to alter access rights to files and directories. All files and directories have security permissions that grant the user particular groups’ or all other users’ access.

To view your files' settings, at the shell prompt type: ls -alt

You should see some files with the following in front of them (an example follows):
total 4
drwxrwsr-x 7 reallyli reallyli 1024 Apr 6 14:30 .
drwxr-s--x 22 reallyli reallyli 1024 Mar 30 18:20 ..
d-wx-wx-wx 3 reallyli reallyli 1024 Apr 6 14:30 content
drwxr-xr-x 2 reallyli reallyli 1024 Mar 25 20:43 files


What do the letters mean in front of the files/directories mean?
r indicates that it is readable (someone can view the file’s contents)
w indicates that it is writable (someone can edit the file’s contents)
x indicates that it is executable (someone can run the file, if executable)
- indicates that no permission to manipulate has been assigned

When listing your files, the first character lets you know whether you’re looking at a file or a directory. It’s not part of the security settings. The next three characters indicate Your access restrictions. The next three indicate your group's permissions, and finally other users' permissions.

Use chmod followed by the permission you are changing. In very simple form this would be:
chmod 755 filename
The example above will grant you full rights, group rights to execute and read, and all others access to execute the file.

  # Permission
  7 full
  6 read and write
  5 read and execute
  4 read only
  3 write and execute
  2 write only
  1 execute only
  0 none

Still confused? Use the table above to define the settings for the three "users." In the command, the first number refers to your permissions, the second refers to group, and the third refers to general users.

Typing the command: chmod 751 filename

gives you full access, the group read and execute, and all others execute only permission.

 

cp Type cp followed by the name of an existing file and the name of the new file.

Ex:
cp newfile newerfile
To copy a file to a different directory (without changing th
e file’s name), specify the directory instead of the new
filename. Ex:
cp newfile testdir
To copy a file to a different directory and create a new file name, you need to specify a directory/a new file name. Ex:
cp newfile testdir/newerfile
cp newfile ../newerfile
The .. represents one directory up in the hierarchy.

file Type file followed by the name of an existing file in the directory.

Ex:
file emergency3_demo.exe

OUTPUT: MS-DOS executable (EXE)

This command allows you to figure out what the file type is and how to use it. For instance the command will tell you whether it is an executable, a compressed file and which type, or something unusual.

This command is simplistic, but often can allow you to determine why a file does not respond the way you expect.

mv Type mv followed by the current name of a file and the new name of the file.

Ex:
mv oldfile newfile

Type mv followed by the name of a file and the new directory where you'd like to place the file. Ex:
mv newfile testdir
This moves the file named newfile to an existing directory named testdir. Be certain you’re specifying a directory
name or the mv command alters the name of the file instead of moving it.

rm Type rm followed by the name of a file to remove the file.

Ex:
rm newfile
Use the wildcard character to remove several files at once. Ex:
rm n*
This command removes all files beginning with n.
Type rm -i followed by a filename if you’d like to be prompted before the file is actually removed. Ex:
rm -i newfile
rm -i n*
By using this option, you have a chance to verify the removal of each file. The -i option is very handy when removing
a number of files using the wildcard character *.

This list only has items related to files, but this link will take you to the page related to commands for directories.