Manipulating Files in Linux
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
Then it may help you to type the command ls -alt to list all of your
current files and directories before you start, so you can see
your directory and files listed.
Already have experience? Then please skip to Commands for Guru Wanna-bees.
All of these commands should work from your command
prompt (regardless which shell you're using). And of course, if
they don't work or help you, I apologize.
Manipulating Files - Linux Commands
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:
You should see some files with the following in front of
them (an example follows):
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
r indicates that it is readable (someone
can view the files contents)
w indicates that it is writable (someone
can edit the files 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 youre looking at a file or a directory.
Its 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'
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.
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
Typing the command:
chmod 751 filename
gives you full
access, the group read and execute, and all
others execute only permission.
cp followed by the name of an existing file and the name
of the new file.
cp newfile newerfile
To copy a file to a different directory (without changing
e files name), specify the directory instead of
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
cp newfile testdir/newerfile
cp newfile ../newerfile
The .. represents one directory up in the hierarchy.
file followed by the name of an existing file in the directory.
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 followed by the current name of a file and the new
name of the file.
mv oldfile newfile
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 youre
specifying a directory
name or the mv command alters the name of the file
instead of moving it.
||Type rm followed by the name of
a file to remove the file.
Use the wildcard character to remove several files at
This command removes all files beginning with n.
Type rm -i followed by a filename if youd 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
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.