Using Basic Filesystem Commands
Using Basic Filesystem Commands I want to introduce you to a few simple commands for getting around the filesystem to start out. If you want to follow along, log in and open a shell. When you log in to a Linux system and open a shell, you are placed in your home directory. As a Linux user, most of the files you save and work with will probably be in that directory or sub-directories that you create.
Table below shows commands to create and use files and directories.
Commands to Create and Use Files
Command Result
cd Change to another directory
pwd Print the name of the current (or present) working directory.
mkdir Create a directory.
chmod Change the permission on a file or directory.
ls List the contents of a directory.
One of the most basic commands you use from the shell is cd. The cd command can be used with no options (to take you to your home directory) or with full or relative paths.
Consider the following commands:
$ cd /usr/share/
$ pwd
/usr/share
$ cd doc
/usr/share/doc
$ cd
$ pwd
/home/chris
The /usr/share option represents the absolute path to a directory on the system. Because it begins with a slash (/), this path tells the shell to start at the root of the file system and take you to the share directory that exists in the usr directory. The doc option to the cd command said to look for a directory called doc that is relative to the current directory. So, that made /usr/share/doc your current directory. After that, by typing cd alone, you are returned to your home directory. If you ever wonder where you are in the filesystem, the pwd command can help you.
Here are a few other interesting cd command options: $ cd ~
$ pwd
/home/chris
$ cd ~/Music
$ pwd
/home/chris/Music
$ cd ../../../usr
$ pwd
/usr
The tilde (~) represents your home directory. So cd ~ takes you there. You can use the tilde to refer to directories relative to your home directory as well, such as /home/chris/Music with ~/Music. While typing a name as an option takes you to a directory below the current directory, you can use two dots (..) to go to a directory above the current directory. The example shown takes you up three directory levels (to /), and then takes you into the /usr directory. The following steps lead you through the process of creating directories within your home directory and moving among your directories, with a mention of setting appropriate file permissions:
1. Go to your home directory. To do this, simply type cd in a shell and press Enter.
2. To make sure that you’re in your home directory, type pwd. When I do this, I get the following response (yours will reflect your home directory):
$ pwd
/home/joe
3. Create a new directory called test in your home directory, as follows:
$ mkdir test
4. Check the permissions of the directory:
$ ls -ld test
drwxr-xr-x 2 joe sales 1024 Jan 24 12:17 test
This listing shows that test is a directory (d). The d is followed by the permissions (rwxr-xr-x).The rest of the information indicates the owner (joe), the group (sales), and the date that the files in the directory were most recently modified (Jan 24 at 12:17 p.m.).
For now, type the following:
$ chmod 700 test
This step changes the permissions of the directory to give you complete access and everyone else no access at all. (The new permissions should read rwx------.)
5. Make the test directory your current directory as follows:
$ cd test
$ pwd
/home/joe/test
If you followed along, at this point a subdirectory of your home directory called test is your current working directory.
Comments
Post a Comment