Home » , » Linux Commands for changing permissions and ownerships

Linux Commands for changing permissions and ownerships

Written By Sajib Barua on Sunday, August 19, 2012 | 12:53 AM

previous Navigating the Linux File System
You may need to change a file’s permission settings to protect it from others. Use the chmod command to change the permission settings of a file or a directory.
To use chmod effectively, you have to specify the permission settings. A good way is to concatenate letters from the columns of Table 4-4 in the order shown (Who/Action/Permission).
 
Table 4-4
Letter Codes for File Permissions
Who
Action
Permission
u (user)
+ (add)
r (read)
g (group)
(remove)
w (write)
o (others)
= (assign)
x (execute)
a (all)
s (set user ID)
You use only the single character from each column — the text in parentheses is for explanation only.
For example, to give everyone read access to all files in a directory, pick a (for all) from the first column, + (for add) from the second column, and r (for read) from the third column to come up with the permission setting a+r. Then use the set of options with chmod, like this:
chmod a+r *.
On the other hand, to permit everyone to execute one specific file, type
chmod a+x filename
Suppose you have a file named mystuff that you want to protect. You can make it accessible to no one but you if you type the following commands, in this order:
chmod a-rwx mystuff
chmod u+rw mystuff
The first command turns off all permissions for everyone, and the second command turns on the read and write permissions for the owner (you). Type ls -l to verify that the change took place. (You see a permission setting of -rw-------.)
Sometimes you have to change a file’s user or group ownership for everything to work correctly. For example, suppose you’re instructed (by a manual, what else?) to create a directory named cups and give it the ownership of user ID lp and group ID sys. How do you it?
Well, you can log in as root and create the cups directory with the command mkdir:
mkdir cups
If you check the file’s details with the ls -l command, you see that the user and group ownership is root root.
To change the owner, use the chown command. For example, to change the ownership of the cups directory to user ID lp and group ID sys, type
chown lp.sys cups
Commands for working with files
To copy files from one directory to another, use the cp command. If you want to copy a file to the current directory but retain the original name, use a period (.) as the second argument of the cp command. Thus, the following command copies the Xresources file from the /etc/X11 directory to the current directory (denoted by a single period):
cp /etc/X11/Xresources
The cp command makes a new copy of a file and leaves the original intact.
If you want to copy the entire contents of a directory — including all subdirectories and their contents — to another directory, use the command cp -ar sourcedir destdir. (This command copies everything in the sourcedir directory to destdir.) For example, to copy all files from the /etc/X11 directory to the current directory, type the following command:
cp -ar /etc/X11
To move a file to a new location, use the mv command. The original copy is gone, and a new copy appears at the destination. You can use mv to rename a file. If you want to change the name of today.list to old.list, use the mv command, as follows:
mv today.list old.list
On the other hand, if you want to move the today.list file to a subdirectory named saved, use this command:
mv today.list saved
An interesting feature of mv is that you can use it to move entire directories (with all their subdirectories and files) to a new location. If you have a directory named data that contains many files and subdirectories, you can move that entire directory structure to old_data by using the following command:
mv data old_data
To delete files, use the rm command. For example, to delete a file named old.list, type the following command:
rm old.list
Be careful with the rm command — especially when you log in as root. You can inadvertently delete important files with rm.
Commands for working with directories
To organize files in your home directory, you have to create new directories. Use the mkdir command to create a directory. For example, to create a directory named images in the current directory, type the following:
mkdir images
After you create the directory, you can use the cd images command to change to that directory.
You can create an entire directory tree by using the -p option with the mkdir command. For example, suppose your system has a /usr/src directory and you want to create the directory tree /usr/src/book/java/examples/applets. To create this directory hierarchy, type the following command:
mkdir -p /usr/src/book/java/examples/applets
When you no longer need a directory, use the rmdir command to delete it.
You can delete a directory only when the directory is empty.
To remove an empty directory tree, you can use the -p option, like this:
rmdir -p /usr/src/book/java/examples/applets
This command removes the empty parent directories of applets. The command stops when it encounters a directory that’s not empty.
Commands for finding files
The find command is useful for locating files (and directories) that meet your search criteria.
When I began using UNIX many years ago (Berkeley UNIX in the early 1980s), I was confounded by the find command. I stayed with one basic syntax of find for a long time before graduating to more complex forms. The basic syntax that I discovered first was for finding a file anywhere in the file system. Here’s how it goes: Suppose you want to find any file or directory with a name that starts with gnome. Type the following find command to find these files:
find / -name “gnome*” –print
If you’re not logged in as root, you may get a bunch of error messages. If these error messages annoy you, just modify the command as follows and the error messages are history. (Or, as Unix aficionados say, “Send ’em to the bit bucket.”)
find / -name “gnome*” -print 2> /dev/null
This command tells find to start looking at the root directory (/) to look for filenames that match gnome* and to display the full pathname of any matching file. The last part (2> /dev/null) simply sends the error messages to a special file that’s the equivalent of simply ignoring them.
You can use variations of this simple form of find to locate a file in any directory (as well as any subdirectories contained in the directory). If you forget where in your home directory you’ve stored all files named report* (names that start with report), you can search for the files by using the following command:
find ~ -name “report*” –print
When you become comfortable with this syntax of find, you can use other options of find. For example, to find only specific types of files (such as directories), use the type option. The following command displays all toplevel directory names in your Linux system:
find / -type d -maxdepth 1 –print
You probably don’t have to use the complex forms of find in a typical Linux system. But if you ever need to, you can look up the rest of the find options by using the following command:
man find
An easy way to find all files that match a name is to use the locate command, which searches a periodically updated database of files on your system. For example, here’s a typical output I get when I type locate Xresources on a Debian system (the output may differ based on your distribution):
/etc/X11/Xresources
/etc/X11/Xresources/xbase-clients
/etc/X11/Xresources/xfree86-common

The locate command isn’t installed by default in some Linux distributions. To install it, open the Add/Remove Software application for your distribution and search for locate. Then select the package from the search results and click Accept to install it.
Commands for mounting and unmounting
Suppose you want to access the files on this book’s companion DVD-ROM when you’re logged in at a text console (with no GUI to help you). To do so, you have to first mount the DVD-ROM drive’s file system on a specific directory in the Linux file system.
Start by looking at the /etc/fstab file for clues to the name of the CD-ROM device. For example, some Linux distributions use the device name /dev/cdrom to refer to CD/DVD-ROM drives, whereas others may use device names such as /dev/hdc, /dev/cdroms/cdrom0, or /dev/cdrecorder (for a DVD/CD-R drive). The entry in /etc/fstab file also tells you the
directory where that distribution expects the CD/DVD to be mounted. Some distributions use /media/cdrom as the mount point, whereas others use /media/cdrom0, or /media/cdrecorder.
It is customary to use the cdrom term to mean both CD-ROM and DVD-ROM.
Log in as root (or type su - to become root), insert the DVD-ROM in the DVD drive, and then type the following command:
mount /dev/hdc /media/cdrom0
This command mounts the file system on the device named /dev/hdc (an IDE DVD/CD-ROM drive) on the /media/cdrom0 directory (which is also called the mount point) in the Linux file system.
After the mount command successfully completes its task, you can access the files on the DVD-ROM by referring to the /media/cdrom0 directory as the top-level directory of the disc. In other words, to see the contents of the DVD-ROM, type
ls -F /media/cdrom0
When you’ve finished using the DVD-ROM — and before you eject it from the drive — you have to unmount the disc drive with the following umount command:
umount /dev/hdc
You can mount devices on any empty directory on the file system. However, each distribution has customary locations with directories meant for mounting devices. For example, some distributions use directories in /mnt whereas others use the /media directory for the mount points.
Commands for checking disk-space usage
You can use two simple commands — df and du — to check the disk-space usage on your system. The df command shows you a summary of disk-space usage for all mounted devices. For example, when I type df on a PC with many mounted storage devices, here’s what I get as output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdb6 28249372 2377292 25872080 9% /
tmpfs 383968 12 383956 1% /dev/shm
/dev/hda5 5766924 1422232 4051744 26% /ubuntu/boot
/dev/hda7 6258100 2989200 2951004 51% /debian/boot
/dev/hda9 5766924 1422232 4051744 26% /ubuntu
/dev/hda10 5766924 1872992 3600984 35% /mepis
/dev/hda11 6258100 2989200 2951004 51% /debian
/dev/hdb3 19558500 1370172 18188328 8% /xandros
/dev/hda2 16087676 10593364 5494312 66% /windows/C
/dev/hdb1 107426620 9613028 97813592 9% /windows/D

This table lists the device, the total kilobytes of storage, how much of that memory is in use, how much is available, the percentage being used, and the mount point.
To see the output of df in a more readable format, type df -h. The output follows:
Filesystem Size Used Avail Use% Mounted on
/dev/hdb6 27G 2.3G 25G 9% /
tmpfs 375M 12K 375M 1% /dev/shm
/dev/hda5 5.5G 1.4G 3.9G 26% /ubuntu/boot
/dev/hda7 6.0G 2.9G 2.9G 51% /debian/boot
/dev/hda9 5.5G 1.4G 3.9G 26% /ubuntu
/dev/hda10 5.5G 1.8G 3.5G 35% /mepis
/dev/hda11 6.0G 2.9G 2.9G 51% /debian
/dev/hdb3 19G 1.4G 18G 8% /xandros
/dev/hda2 16G 11G 5.3G 66% /windows/C
/dev/hdb1 103G 9.2G 94G 9% /windows/D

If you compare this output with the output of the plain df (the preceding listing), you see that df -h prints the sizes with terms like M for megabytes and G for gigabytes. These are easier to understand than 1K-blocks.
The other command, du, is useful for finding out how much space a directory takes up. For example, type du /etc/X11 to view the contents of all the directories in the /etc/X11 directory. (This directory contains X Window System configuration files.) You see the following:
12 /etc/X11/Xresources
36 /etc/X11/Xsession.d
272 /etc/X11/app-defaults
20 /etc/X11/cursors
12 /etc/X11/xinit
. . . lines deleted . . .
12 /etc/X11/fonts/misc
8 /etc/X11/fonts/100dpi
8 /etc/X11/fonts/75dpi
8 /etc/X11/fonts/Speedo
8 /etc/X11/fonts/Type1
48 /etc/X11/fonts
2896 /etc/X11

Each directory name is preceded by a number — which tells you the number of kilobytes of disk space used by that directory. Thus, the /etc/X11 directory uses 2896KB (or about 2.9MB) disk space. If you simply want the total disk space used by a directory (including all the files and subdirectories contained in that directory), use the -s option, as follows:
du -s /etc/X11
2896 /etc/X11
The -s option causes du to print just the summary information for the entire directory.
Just as df -h prints the disk-space information in megabytes and gigabytes, you can use the du -h command to view the output of du in more readable form. For example, here’s how I combine it with the -s option to see the space that I’m using in my home directory (/home/edulaney):
du -sh /home/edulaney
645M /home/edulaney
next Introducing Linux Applications
Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger