LINux tutorial pt.2BASIC COMMANDS AND COMMAND LINE THE SHELL The shell is a program that takes the commands you send to the OS to perform. Much like the ‘Terminal’ or ‘Console’ in a GUI OS, Linux Shell is just another shell that is launched for you. I will be using the shell program known as bash (Bourne Again SHell) that nearly all Linux distros default to. There are other shells of course other than bash like: · Ksh · Zsh · Tsch We aren’t going to get into these but just know they exist. There are multiple distros but most of them should be somewhat the same. Nearly all distros will use this: username@host:dir dave@dbmventures:/home/dave $ Notice the $ at the end of the prompt? Different shells will have different prompts, in our case the $ is for a normal user using Bash, Bourne or Korn shell, you don't add the prompt symbol when you type the command, just know that it's there. We can start out by using the echo command that just simply prints out the arguments for you to display: $ echo Hello World I am going to just go ahead and get into the commands that are generally the most used. I will not be using every command (that would take an eternity) but hopefully the most useful and most used commands: 1. Sudo - Short for superuser do, sudo is one of the most popular basic Linux commands that lets you perform tasks that require administrative or root permissions. Sudo is also one of the most basic commands that there is. It requires administrative or root permissions. While using the commands, the system will prompt you to authentication by use of a username and password. Once you enter the information for authentication, it will log a timestamp as a tracker and by default every root user can run sudo commands for 15 min/session. Here is the general sytax: sudo (command) You can also add an option, such as:
2. Pwd (print working directory) – Alright so try to keep this in mind, EVERYTHING in Linux is a file and you will understand this the further you go into Linux. Picture the file tree hierarchy and this will help you navigate through files, directories, and processes. So, starting from the beginning, the first directory in the filesystem will be called root. The root directory has many folders and files which you can store more folders and files, etc. Here is an example of what the directory tree looks like: / |-- bin | |-- file1 | |-- file2 |-- etc | |-- file3 | `-- directory1 | |-- file4 | `-- file5 |-- home |-- var Like an URL in the browser where you enter the domain name or in the file manager where you have to select which folder your pictures are in, where your music is, or where a video game is within the folders, you must navigate through the ‘paths’ to find what you want within Linux as well. If you had a folder named home with a folder inside of it named dave and another folder in that folder called Movies, that path would look like this: /home/dave/Movies, pretty simple right? So, to help you know what directory you are in type ‘pwd’ into the terminal. Pwd simply means ‘print working directory. So all you have to do is type ‘pwd’ and it will show where you are. 3. Cd (change directory) – In order to navigate through these directories that was just discussed in the pwd area you will use the ‘cd’ command that allows you to change the directory, thus cd means change directory. There are two (2) ways to specify a path and that is through absolute and relative paths. · Absolute path: This is the path from the root directory. The root is the head honcho. The root directory is commonly shown as a slash. Every time your path starts with / it means you are starting from the root directory. For example, /home/dave/Desktop. · Relative path: This is the path from where you are currently in filesystem. If I was in location /home/dave/Documents and wanted to get to a directory inside Documents called taxes, I don’t have to specify the whole path from root like /home/dave/Documents/taxes, I can just go to taxes/ instead. Here are some other examples that might help you a little more Let’s say you’re in /home/username/Documents and want to go to Photos, a subdirectory of Documents. To do so, enter the following command: cd Photos If you want to switch to a completely new directory, for example, /home/username/Movies, you have to enter cd followed by the directory’s absolute path: cd /home/username/Movies Now from this directory you have a folder called Hawaii that you can navigate to by doing this command: cd Hawaii Now from this directory you have a folder called Hawaii that you can navigate to by doing this command: cd Hawaii As you may have noticed it get a little taxing on you having to type all this out. Lucky for us we don’t have to type it out every single time, especially just going forward or backward in directories. Check these options out here: · (current directory). This is the directory you are currently in. · .. (parent directory). Takes you to the directory above your current. · ~ (home directory). This directory defaults to your “home directory”. Such as /home/dave. · (previous directory). This will take you to the previous directory you were just at. $ cd . $ cd .. $ cd ~ $ cd - 4. Ls – is a way to view the files in directories within a system. Running it without flags or parameters will show you the current folder that you are in. Here is the cool part, you can also lookin in other directories without having to change from your current directory to the one you would like to “peak” into to show the content in another. All you must do to see a specific folder is this: $ ls $ ls /home/dave Here are some options you can use with the ls command: · ls -R - lists all the files in the subdirectories. · L -l – for long form listing in the directory · ls -a - shows hidden files in addition to the visible ones. · ls -lh - shows the file sizes in easily readable formats, such as MB, GB, and TB. · Ls -la – is basically putting together ‘l’ and ‘a’ to be shown as one. ‘l’ is for long and ‘a’ is for all. $ ls $ ls /home/dave 5. Touch – The touch command is for creating an empty file or generate and modify a timestamp in the Linux command line. Since this is the only thing, right now, we are going to use it for here is the example of touch: touch /home/username/Documents/Web.html 6. cat – The cat command is also known as concatenate, and this is also one of the most used Linux commands. What this command does for you is it lists, combines, and writes content to the standard output. To run the cat command simply type cat followed by the file name and its extension. For example: cat filename.txt Here are a few other ways that you can use the cat command: Cat > filename.txt creates a new file Cat filename.txt filename2.txt > filename3.txt and it stores the output in filename3.txt Tac filename.txt displays content in reverse order 7. cp – The cp command stands for copy. This command is like the copy and paste feature in Windows except you must type these commands out into the command line. For example: cp mycoolfile /home/dave/Documents/cooldocs mycoolfile is the file you want to copy and /home/dave/Documents/cooldocs is where you are copying the file to. You can copy multiple files and directories as well as use wildcards. A wildcard is a character that can be substituted for a pattern-based selection, giving you more flexibility with searches. You can use wildcards in every command for more flexibility. · * the wildcard of wildcards, it's used to represent all single characters or any string. · ? used to represent one character · [] used to represent any character within the brackets $ cp *.jpg /home/dave/Pictures This will copy all files with the .jpg extension in your current directory to the Pictures directory. A useful command is to use the -r flag, this will recursively copy the files and directories within a directory. Try to do a cp on a directory that contains a couple of files to your Documents directory. Didn’t work did it? Well that’s because you’ll need to copy over the files and directories inside as well with -r command. $ cp -r Pumpkin/ /home/dave/Documents One thing to note, if you copy a file over to a directory that has the same filename, the file will be overwritten with whatever you are copying over. This is no bueno if you have a file that you don’t want to get accidentally overwritten. You can use the -i flag (interactive) to prompt you before overwriting a file. $ cp -i mycoolfile /home/dave/Pictures 8. mv – mv means move, but it also used for renaming files. This command is very similar to the cp command as far as tags and functionality goes. Here are a few examples: renaming files $ mv oldfile newfile Or to move a file to a different directory $ mv file2 /home/dave/Documents Move more than one file $ mv file_1 file_2 /somedirectory Rename directories as well $ mv directory1 directory2 Much like cp, if you mv a file or directory it will overwrite anything in the same directory. So you can use the -i flag to prompt you before overwriting anything. $ -i directory1 directory2 et’s say you did want to mv a file to overwrite the previous one. You can also make a backup of that file and it will just rename the old version with a ~. $ mv -b directory1 directory2 9. history – You can use this command to see your history, which is very helpful when you need to look where files were commanded to go, directories renamed, and basically anything you have typed in this session. You can also use clear to clear your cluttered session. Also a quick shortcut is ctrl-R which shows a reverse search. 10. file - Normally you would expect a file called banana.jpeg and expect a JPEG picture file. In Linux, filenames aren’t required to represent the contents of the file. You can create a file called funny.gif that isn’t a really a GIF. To find out what kind of file a file is, you can use the file command. It will show you a description of the file’s contents. $ file banana.jpg 11. mkdir - We’re gonna need some directories to store all these files we’ve been working on. The mkdir command (Make Directory) is useful for that, it will create a directory if it doesn’t already exist. You can even make multiple directories at the same time. $ mkdir books paintings You can also create subdirectories at the same time with the -p (parent flag). $ mkdir -p books/hemmingway/favorites 12. help - Linux has some great built-in tools to help you how to use a command or check what flags are available for a command. One tool, help, is a built-in bash command that provides help for other bash commands (echo, logout, pwd, etc). $ help echo This will give you a description and the options you can use when you want to run echo. For other executable programs, it’s convention to have an option called --help or something similar. $ echo --help Not all developers who ship out executables will conform to this standard, but it’s probably your best shot to find some help on a program. As you already know, these are not the only functions that you can use as it is always expanding as people create more and “terminals” of Linux and the developers who work for companies around the world. I am adding a list of a few web sites, organizations, and companies have. I could copy and paste the entire lists but to save a bit of time and not stealing copyrighted lists I will provide you with the resources I used: Hostinger - https://www.hostinger.com/tutorials/linux-commands Linux Journey - https://linuxjourney.com/ PhoenixNAP - https://phoenixnap.com/kb/linux-commands ss64 - https://ss64.com/bash/ HaydenJames - https://haydenjames.io/90-linux-commands-frequently-used-by-linux-sysadmins/
Comments
|
About - GN.NetworkGNN Interactive was created by professional e-sports athletes, influential figures, and developers to provide services and products for the gaming market. Archives
September 2021
Categories |