| 121 | == Playing with Linux == |
| 122 | |
| 123 | === Working with directories === |
| 124 | |
| 125 | A brief overview of the most common commands to work with directories: |
| 126 | '''pwd''', '''cd''', '''ls''', '''mkdir''' and '''rmdir'''. These commands are available on any Linux (or Unix) |
| 127 | system. |
| 128 | |
| 129 | - '''pwd''' : Print Working Directory (Will tell you the location you are currently working) |
| 130 | - '''cd''' : You can change your current directory with the cd command |
| 131 | * '''cd''' : shortcut to get back into your home directory. Just typing cd without a target directory, will put you in your home directory |
| 132 | * '''cd ..''' : To go to the parent directory (the one just above your current directory in the directory tree) |
| 133 | * '''cd -''' : To go to the previous directory |
| 134 | - '''ls''' : You can list the contents of a directory with ls |
| 135 | * '''ls -a''' : To show all files. Showing all files means including the hidden files. When a file name on a Linux file system starts with a dot, it is considered a hidden file and it doesn't show up in regular file listings. |
| 136 | * '''ls -l''' : to display the contents of the directory in different formats or to display different parts of the directory. |
| 137 | * '''ls -lh''' : shows the numbers (file sizes) in a more human readable format. |
| 138 | - '''mkdir''' : Create new directories |
| 139 | - '''mkdir -p''': To create parent directories as needed |
| 140 | - '''rmdir''': To remove the directory. (Directory has to be empty) |
| 141 | |
| 142 | '''Some exercise''' |
| 143 | 1. Login to your VM. and Display your current directory |
| 144 | 2. Change to /etc directory and display current directory |
| 145 | 3. Go to root directory and list the contents |
| 146 | 4. List a long listing of the root directory |
| 147 | 5. Go to your home directory |
| 148 | 6. Make directory named 'test' |
| 149 | 7. make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named '.hidden'. Make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named 'unhidden'. |
| 150 | 8. Go to 'one' directory and list the content. |
| 151 | 9. Then list all contents |
| 152 | 10. Remove directory 'unhidden' |
| 153 | 11. Go to your home and try to remove directory 'test' |
| 154 | |
| 155 | {{{ |
| 156 | dilum@DilumL:~$ pwd |
| 157 | /home/dilum |
| 158 | dilum@DilumL:~$ cd /etc |
| 159 | dilum@DilumL:/etc$ pwd |
| 160 | /etc |
| 161 | dilum@DilumL:/etc$ cd .. |
| 162 | dilum@DilumL:/$ ls |
| 163 | bin etc lib lost+found opt run var |
| 164 | boot home lib32 media proc sbin tmp mnt |
| 165 | dev lib64 root sys usr |
| 166 | dilum@DilumL:/$ ls -l |
| 167 | total 88 |
| 168 | drwxrwxr-x 2 root root 4096 Mar 4 2016 bin |
| 169 | drwxr-xr-x 3 root root 4096 Feb 9 2016 boot |
| 170 | drwxr-xr-x 20 root root 3360 Nov 16 08:16 dev |
| 171 | drwxr-xr-x 155 root root 12288 Aug 29 09:30 etc |
| 172 | drwxr-xr-x 4 root root 4096 Aug 16 08:54 home |
| 173 | drwxr-xr-x 19 root root 4096 Mar 4 2016 lib |
| 174 | drwxr-xr-x 2 root root 4096 Mar 4 2016 lib32 |
| 175 | drwxr-xr-x 2 root root 4096 Mar 4 2016 lib64 |
| 176 | drwx------ 2 root root 16384 Dec 15 2015 lost+found |
| 177 | drwxr-xr-x 4 root root 4096 Dec 15 2015 media |
| 178 | drwxr-xr-x 2 root root 4096 Dec 15 2015 mnt |
| 179 | drwxr-xr-x 3 root root 4096 Jan 26 2016 opt |
| 180 | dr-xr-xr-x 189 root root 0 Nov 16 08:16 proc |
| 181 | drwx------ 12 root root 4096 Sep 6 16:25 root |
| 182 | drwxr-xr-x 26 root root 960 Nov 16 08:41 run |
| 183 | drwxr-xr-x 2 root root 4096 Mar 4 2016 sbin |
| 184 | dr-xr-xr-x 13 root root 0 Nov 16 08:16 sys |
| 185 | drwxrwxrwt 18 root root 4096 Nov 16 12:50 tmp |
| 186 | drwxr-xr-x 12 root root 4096 Nov 8 12:39 usr |
| 187 | drwxr-xr-x 13 root root 4096 Dec 16 2015 var |
| 188 | dilum@DilumL:/$ cd |
| 189 | dilum@DilumL:~$ pwd |
| 190 | /home/dilum |
| 191 | dilum@DilumL:~$ mkdir test/one/.hidden |
| 192 | mkdir: cannot create directory ‘test/one/.hidden’: No such file or directory |
| 193 | dilum@DilumL:~$ mkdir -p test/one/.hidden |
| 194 | dilum@DilumL:~$ mkdir -p test/one/unhidden |
| 195 | dilum@DilumL:~$ cd test/one |
| 196 | dilum@DilumL:~/test/one$ ls |
| 197 | unhidden |
| 198 | dilum@DilumL:~/test/one$ ls -a |
| 199 | . .. .hidden unhidden |
| 200 | dilum@DilumL:~/test/one$ rmdir unhidden/ |
| 201 | dilum@DilumL:~/test/one$ ls -a |
| 202 | . .. .hidden |
| 203 | dilum@DilumL:~$ rmdir test |
| 204 | rmdir: failed to remove ‘test’: Directory not empty |
| 205 | |
| 206 | }}} |
| 207 | |