I will try to keep updating this post with useful Terminal shortcuts and commands I find helpful. So bookmark this one and leave any commands you think should be added in the comments and I'll move them to the main post.
sudo !!
This runs the last command you typed but runs it as sudo. If you typed a long command and forgot to start it with sudo or didn't know you needed sudo, instead of retyping the whole thing or using UP ARROW to paste the command back in, just type sudo!! and hit ENTER. This will add the sudo and re-run the command as soon as you press the ENTER key.
CTRL x e
To use this just hold the CTRL key, press x, then press e while still holding down the CTRL key. This will open the nano text editor where you can type out whatever commands you want to run in Terminal even multi lines. Think of it as a one use bash script file. When you exit nano your typed in commands will then run in your open Terminal window.
ls -h --group-directories-first --color=auto
This command displays a directory list in colors and puts folders first, organizing them in ascending alphabetical order. Following the folders are the files in said directory also listed in ascending alphabetical order. It's a little long so you may want to open a text editor and save it as a bash script, that's what I did, so now I can just type l (lower case L) in my terminal and a nice colored, folder first, directory list appears. HERE is a quick guide on how to write a bash script, make it executable, and add it's folder to your PATH. The only thing I notice that this tutorial didn't say was you SAVE your script file as a .sh files in a folder that is in your PATH.
mv FileName.txt NewFileName.txt
To RENAME a file you use the command mv for some reason. Simple repalce FileName.txt with the file name you want to rename and replace NewFileName.txt with the name you want the file to have. The mv command also functions as a MOVE command too. To use it as a MOVE command just add a destination directory path in front of the NewFileName.txt part. i.e.
mv FileName.txt /home/UserName/Documents/NewFileName.txt
This will move the FileName.txt to /home/UserName/Documents/ directory and rename the file to NewFileName.txt if you don't want to rename the file and just want to move it, then just make the file name the same as it was (mv FileName.txt /home/UserName/Documents/FileName.txt)
Some simple commands I forget:
- ~ is a shortcut or substitution for the directory /home/userName/ think of it as the root of your home directory.
- cp FileName.txt ~/Documents/ This copies FileName.txt to the Documents folder in your home directory. cp FileName.txt ~/Documents/NewFileName.txt would copy and rename FileName.txt to NewFileName.txt and put it in your Documents folder.
- find . -name "*.bak" -type f -delete This deletes all files with the .bak extension in your current directory and in all sub-directories below your current directory. BEFORE USING THIS YOU SHOULD RUN find . -name "*.bak" -type f TO SEE THE LIST OF ALL FILES YOU WILL BE DELETING. USE WITH CAUTION.
- rm -r DirectoryName This will remove a directory and everything below it. This is a recursive delete of a directory or directories and all their files. USE WITH CAUTION.
- rmdir dir1 is a safer command that will remove empty directories but will give you an error if dir1 has files or sub-directories in it. This command deletes the empty directory called dir1 .
chmod options permissions file_name
Examples
- chmod u=rwx,g=rx,o=r myfile.txt
- chmod 754 myfile.txt
- chmod -R 755 /PATH/TO/DIRECTORY
In the first Example above;
Members of Current User's Group(g=) can read(r) and execute(x) myfile.txt
Others(o=) may only read(r) the file myfile.txt
If you wanted to, you could add r,w and/or x 's to the g= or o= part of that command or take away either r, w, or x from the u= part of that example, BUT YOU SHOULDN'T. Very, very, rarely do want to give USER, GROUP and OTHERS all permission or take all permission away for all. With the wrong combinations of r,w, and/or x you could easily lock yourself and everyone else out of access to a file by not giving enough permission to the file or cause security risks by giving too many permissions to your files. The above example is the most common permissions you will find on user's files (home directories or additional hard drives storage space). The System Files are another matter and you should read a more in depth article before playing with those permissions.
In the second Example above;
This means the exact same thing as the first example, it's just using octal permissions notation. Here is how it works;
The first position of the 3 digit number, represents the user(u=), the 2nd digit position represents the group(g=), the 3rd digit position represents others(o=). The actual numbers in those positions are derived from the following table;
4 stands for "read"
2 stands for "write"
1 stands for "execute"
0 stands for "no permission."
By simply adding the above numbers together you will get a single digit number that will represent the permissions you want the User, Group and Others to have. Lets say you want to give User full access (read, write and execute) and you want Group's to just have read access (read) and you want Others to have any write and read but not execution. All you do is add; you want User to read (4) + write(2) + execute(1). Well that's 4+2+1=7. So our first number is 7. You want Group to have only read. Well that's just 4. So our second digit is 4. You also want Others to have read and write, but no execute. Well that's read(4) + write(2). So it's 4 + 2 = 6. Our last digit is 6. You now have your three digit number, 746. Your command to set those permissions on myfile.txt would be chmod 746 myfile.txt.
In the third example above we uses an Option;
In the third example we want to change the permissions on all the files and folders and subfolders/subfiles in the directory located at /PATH/TO/DIRECTORY. This is called a Recursive Change. So if we run the command chmod -R 777 Documents from our Home directory, this would change the permissions of every files, folder, subfolder, and subfiles in the Documents directory allowing everyone full unrestricted access to everything contained in the Documents directory (Not a good Idea BTW). When using the -R option with chmod you should check all the subfolders and subfiles you'll be changing. You can use chmod in combination with find for a more selective recursive like change (see chmod Helper Commands below).
Of special note on options, chmod -r 777 Documents means something completely different from chmod -R 777 Documents CASE MATTERS -R means Recursive, -r means take away read permissions
Options for chmod
-c, --changes Like --verbose, but gives verbose output only when a change is actually made.
-f, --silent, --quiet Quiet mode; suppress most error messages.
-v, --verbose Verbose mode; output a diagnostic message for every file processed.
--no-preserve-root Do not treat '/' (the root directory) in any special way, which is the default setting.
--preserve-root Do not operate recursively on '/'.
--reference=RFILE Set permissions to match those of file RFILE, ignoring any specified MODE.
-R, --recursive Change files and directories recursively.
--help Display a help message and exit.
--version Output version information and exit.
Show a single file called myfile.txt and displays its permissions. Here is what that output will look like;
This will list all the files in your current directory and show you their permissions. This an easy way to the permissions from a bunch of files all at once.
This example will make all .sh files in the current directory executable.
The chown command above changes the Ownership of every file, subdirectory, and subfile in the directory DirectoryToOwn to be owned by the user UserName and by the group UserGroup. Most of the time UserName and UserGroup are the same. No space before or after the colon (:). Leave out the -R and replace DirectoryToOwn with a single file name to just assign ownership of a single file or combine chown with find command (above) for a selective group of files.
sudo blkid
This list all of your hard drive or other mounted drives (flash, etc..) on your system. You can use this command to find the uuid of a hard drive or flash drive to use in your fstab file for auto-mounting drives on system boot.
sudo dmidecode --type 17
Want to know what kind of memory is in your system? This handy command will tell you way more information than you'll ever want. I need to know my manufactures name and this command returned that information under the TYPE heading.
No comments:
Post a Comment