Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Useful Linux

Dylan Christopherson edited this page Jul 30, 2018 · 49 revisions

Useful Linux Commands!

Use with care

sudo -s

Important info

ethtool -P eth0    //MAC address (Permanent address)
hostname
hostname -I

uname -m           //Kernel information
uname -s
uname -r

ip addr

cat /etc/os-release

echo $0            //Tells what type of shell you are using

Files and Directories

mv item1 item2       //Performs both file moving and file renaming depending on how it is used
mv dir1 dir2         //Moves all contents of dir1 into dir2 and deletes dir1

du -sh directoryName //Gets the size of all files in directory

Managing users

useradd <user>
passwd <user>
userdel <user>     //Only deletes users account
userdel -r <user>  //Removes user, home folder, and their files

getent group wheel
usermod -aG wheel <user>
su - <user>        //Switch users

Directories

rm -r -f <directory>           //Removes all files from directory
cp -a /source/. /dest/         //-a preserves all file attribures and symlinks
                               //. at the end allows to copy all files and folders including hidden ones
chown -R username:groupname *    Ex: chown -R jenkins:users *    //changes all ownership in file

cd -                           //Changes directory to previous working directory

List Stuff

ls -d */                       //List directories in a directory

Adding to Path

echo $PATH
PATH=$PATH:<Directory path>
export PATH

export PATH=$PATH:<Directory path>                     //All in one command    

echo 'PATH=$PATH:/opt/openmpi/bin' >> /etc/bashrc      //Another way to add path

Symlinks

ln -s /full/path/to/file /full/path/to/link/area    --> ln -s source destination

Ex:    ln -s /home/dylan/aws-pmix/mysqlauth.sh /home/dylan/aws-pmix/backend/shared/mysqlauth.sh

ls -Al    //In directory you created symlink. Should look something like this:
lrwxrwxrwx 1 dylan users   47 Jun 12 12:02 mysqlauth.sh -> /home/dylan/aws-pmix-scale-testing/mysqlauth.sh

The arrow pointing to the file path is the symlink. To delete a symlink, go into the directory with the symlink and:

rm fileName
Ex:  rm mysqlauth.sh

Helpful commands

type <command>                //Tells you what type of command it is    
file <filename>               //Tells you what type of file it is
printenv | less               //Prints environment variables
which                         //Determines exact location of given executable

apropos searchTerm            //Searches the list of man pages based on search terms

alias _name_='_string_'       //How to create an alias
unalias _name_

history
!8            //Runs the 8th line in the history output
ctrl-r        //Begins searching history. Type to search. Also, to go to next matching item
ctrl-j        //Copies current line in command history to command line  

Date

date
time

https://stackoverflow.com/questions/21721875/store-date-variable-in-bash-script

Less Command

Page Up or b         Scroll back one page
Page Down or space   Scroll forward one page
Up Arrow             Scroll up one line
Down Arrow           Scroll down one line
G                    Move to the end of the text file
1G or g              Move to the beginning of the text file
/_characters_        Search forward to the next occurrence of _characters_
?_characters_        Search backward to the next occurrence of _characters_
n                    Search for the next occurrence of the previous search
h                    Display help screen
q                    Quit less

Directories in Linux

Under "3-Exploring The System" in The Linux Command Line book

/            The root directory. Where everything begins.
/bin         Contains binaries (programs) that be must present for the system to boot and run
/boot      
/dev         This is a special directory which contains device nodes. “Everything is a file” also applies to devices. Here is where the kernel maintains a list of all the devices it understands.

/etc
/home
/lib          Contains shared library files used by the core system programs. These are similar to DLLs in Windows.
/lost+found
/media
/mnt
/opt
/proc
/root           This is the home directory for the root account.
/sbin           This directory contains “system” binaries. These are programs that perform vital system tasks that are generally reserved for the superuser.
/tmp            The /tmp directory is intended for storage of temporary, transient files created by various programs. Some configurations cause this directory to be emptied each time the system is rebooted.

/usr            The /usr directory tree is likely the largest one on a Linux system. It contains all the programs and support files used by regular users.
/usr/bin         
/usr/lib        The shared libraries for the programs in /usr/bin.
/usr/local
/usr/sbin       Contains more system administration programs.
/usr/share
/usr/share/doc

/var              With the exception of /tmp and /home, the directories we have looked at so far remain relatively static, that is, their contents don't change. The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, etc. are located here.

/var/log          /var/log contains log files, records of various system activity. These are very important and should be monitored from time to time. The most useful ones are /var/log/messages and/or /var/log/syslog. Note that for security reasons on some systems, you must be the superuser to view log files.

Cursor Movement Commands (When using the terminal)

Ctrl-a      Move cursor to the end of the line.   
Ctrl-e      Move cursor to the beginning of the line.
Ctrl-f      Move cursor backward one character; same as the left arrow key.
Ctrl-b      Move cursor forward one character; same as the right arrow key.
Alt-f       Move cursor forward one word.
Alt-b       Move cursor backward one word.
Ctrl-l      Clear the screen and move the cursor to the top left corner. The clear command does the same thing.

Cutting and pasting (killing and yanking) text

Ctrl-k         Kill text from the cursor location to the end of line.
Ctrl-u         Kill text from the cursor location to the beginning of the line. 
Alt-d          Kill text from the cursor location to the end of the current word.
Alt-Backspace  Kill text from the cursor location to the beginning of the current word. If the cursor is at the beginning of a word, kill the previous word.
Ctrl-y         Yank text from the kill-ring and insert it at the cursor location

Vi (Vim)

l        Right
h        Left
j        Down
k        Up
0 (zero) Beginning of current line
$        End of current line
o (letter) Adds line below current line
O (letter) Adds line above current line
dd       Deletes current line
dG       Deletes from current line to EOF
u        Undo. Only up to a certain point
d$       Cursor position to end of line
yy       Copies a line
p        Pastes
/        Allows for searching (similar to less)
:%s/Line/line/g       Replaces Line with line throughout the file
Clone this wiki locally