| BASH Beginner's Tutorial |
|
|
|
| Written by Jordan | |
|
Objective This tutorial aims to ease you into getting familiar with the BASH shell by covering the basic usage of the BASH shell.BASH, Bourne Again SHell, is the GNU implementation of the Bourne Shell (sh) written for UNIX by Stephen Bourne. A shell is nothing but an interface to the user over the Linux Kernel. So its a gateway to interacting with the system. As it is command based its often referred to as a CLI (Command Line Interface). Though the commands look daunting at first, once you get the hang of things you will be amazed with the speed with which you can accomplish things, much faster than a GUI. The shell can be invoked using konsole from KDE or gnome-terminal in the GNOME desktop environment. You will find there a '$' or a '#', depending if you are logged in not as or as root, awaiting your instructions. The shell is basically, as said before, a command line interpreter. So each command (line) is taken in by the shell, checked for usage correctness and the corresponding functions undertaken. BASH tends to be terse, a trait it inherits from its UNIX ancestor (sh). Terse in the sense of usually not prompting anything if you command completed without errors. So no news is good news here! So lets start some practical sessions: Prompt This prompt signifies that i am logged in as user demouser and my computer name is homepc. It also says that i am in the directory ~/example. '~' stands for the home directory of the user. So '~/example' is nothing but a directory named example inside the home directory. Listing Contents In Linux, as in UNIX, all entities are files. So even directories, devices are considered as files.Okay so lets find out the contents of your folder. A simple 'ls' command lists the contents: demouser@homepc:~/example$ ls As you can see that we have three directories (archive, docs and temp) and a single file named nameList. Lets not base our decision on the color of the names printed in. Here is something more descriptive: demouser@homepc:~/example$ ls -l Okay so now have a whole lot of information, lets understand it step by step. The first column deals with the file type and the permissions. It is followed by the owner of the files, ie demouser. Next we have is the group to which demouser belongs to, and that is the root group. So basically demouser is also the administrator of the system. Then are the file sizes, last date of modification and eventually the names. Filetype d indicates directory - indicates normal file l indicates its a link to other file Permissions r indicates read w indicates write x indicates execute These permissions dictate what can owners, groups and other users are allowed do with the file. A '-' indicates the corresponding capability is not allowed to the specified role (user, owner or group). So, -rwxr-xr--, signifies that; its a file (the leading '-'); Owner has all permissions (read, write and execute);people in the same group as the owner have read and execute permissions and other users have just read permissions. So basically after the first character read in groups of three. More on permissions later. Lets unearth what is present in all the directories under example. Using the '-R' (recursive) option you can make 'ls' go into every directory and print it's contents. demouser@homepc:~/example$ ls -R The output now shows us the contents of all the folders under example. The list prints till it does not find a file. In Linux, a hidden file start its name with a dot '.'. Let's see if there are any hidden files with the '-a' option: demouser@homepc:~/example$ ls -a We get to know that there is a hidden file by the name .config Creating FilesLets move on to now creating a file using the cat command: demouser@homepc:~/example$ cat > newfile demouser@homepc:~/example$ cat newfile The first command instructs the shell to store all the keyboard input to a file called newfile. You can end the input by pressing the combination of Control-D. To see the contents of the file just invoke the cat command with the filename and contents are outputted on the screen. Using Less
At times it can happen that you file contents are beyond one screen full. This causes you to loose out on the initial text. To fix this issue a command called less can be used Directory manipulationsdemouser@homepc:~/example$ mkdir testdir Creates a directory called testdir. At times you want to create a whole tree of directories. For that you can use the '-p' option. demouser@homepc:~/example$ mkdir -p dir1/dir2/dir3/ The first command created the directory structure with dir3 insider dir2 which is sub-folder of dir1. To confirm that the command worked as expected, just list recursively the contents of dir1. Removing directories demouser@homepc:~/example$ rmdir testdir The rmdir command works well only when deleting empty directories. In our case, testdir was empty so things went off well, but as dir1 was not empty rmdir gave out an error. The issue can be fixed by using the rm command: demouser@homepc:~/example$ rm -ir dir1/ rm coupled with the '-i' and '-r' options, asks you if the file is to be deleted as it traverses recursively. Changing permissionsAs mentioned above that permissions govern what powers the users have over a file. Let us try out so permission changing commands using chmod: demouser@homepc:~/example$ chmod +x newfile The first command adds a 'x', ie execute permission th newfile. See as the subsequent output has all the execute bits on. Then we remove the 'r', ie read permissions. So all the r's have been replaced with '-', signifying no reed permissions for anyone. Lets now try to change permissions for specific users and not all the users. Read, write and execute are assigned with numerical weights of 4,2 and 1 respectively. Show below: r = 4 w = 2 x = 1 So a file with all permissions on has the value r+w+x=4+2+1 As mentioned above the permissions cater to three sets of people - owner, group the owner is in and all the other users. So below are few permission codes and their explanation:
Need Help/Questions/Comments? Need help with something or just want to make a comment? Post it in our forum! |
| < Prev | Next > |
|---|