My commandline notes - majuma/Commandline GitHub Wiki

Identify computers name: hostname

Make a directory(folder): (mkdir (folder name?)

E.g.
mkdir temp (makes a directory called temp)
mkdir terminal(makes a directory called terminal)
mkdir *terminal/wanjala* (makes a directory called wanjala inside a directory called terminal)
mkdir *"i like this"* (makes a directory with a spaced name)
mkdir -p *term/me/you/us* (mkdir -p will make an entire path even if all the directories don't exist! very convenient, good for my nerves!)

Change directory: (cd (folder name?))

First navigate into a folder whose content you would like to change. Use pwd (print working directory) to display the name of the current directory you are working in.
E.g.
pwd - use this to know your current location
ls – list the contents in the folder you are currently in
Move up path
cd ~/ (takes you back to the parent directory)
cd .. (takes you one step back out of you current folder
cd ../../ (takes you two steps back out of you current folder)
cd ../../../ (takes you three steps back out of you current folder)
Move down path
cd terminal (opens up the folder called terminal within the directory you’re currently in)
cd terminal/wanjala (moves up folders from temp through terminal and into the folder wanjala)

Delete a directory (rmdir (folder name?))

rmdir – remove directory
E.g.
rmdir wanjala (delete the folder named wanjala)

Moving around

pushd - "Save where I am, then go here."
Popd – takes you back to the folder you were in before the pushd command (takes the last directory you pushed and "pops" it off, taking you back there)
E.g.
cd temp - Start from folder termed temp
$ mkdir -p i/like/icecream (starting from the folder temp creates the folders I then Like then Icecream, i.e. creates the folder path i/like/icecream inside the folder temp)
$ pushd i/like/icecream (save my current location (temp) then follow folder path i/like/icecream)
$ pwd
$ popd

Making empty files (touch (filename.file version))

E.g.
mkdir funny (create new folder)
cd funny (change into new folder)
touch funny.txt (create new text file inside the new folder)
cd .. (change one level down the new folder)
rmdir funny (try to delete the new folder)
error message received :- rmdir: funny: Directory not empty

Copy a file (cp)

cp – copy a file
cp –r - copy a folder with files in it
E.g.
cp ilikeyou.txt sweet.txt (copy a new text file called sweet in the same location as the text file ilikeyou)
cp sweet.txt hot.txt (copy a new text file called hot in the same location as the text file sweet)
cp hot.txt feel.txt (copy a new text file called feel in the same location as the text file hot)
mkdir something (make a directory called something in the folder temp)
cp hot.txt something/(copy the text file called hot in to the folder something)
ls something (look inside the folder called something)
hot.txt
cp -r something newplace (copy the contents in the folder something into a new folder called newplace)
ls newplace
hot.txt
ls something
hot.txt

Moving files (renaming files)

Moving files or, rather, renaming them. It's easy: give the old name and then the new name. mv oldname newname
E.g.
mv neat.txt dirty.txt (rename the text file neat to dirty)
ls newplace
awesome.txt hot.txt
mv newplace myplace (rename the folder newplace to myplace)
ls myplace
awesome.txt hot.txt

View a file (more or less)

Instructions: Open your text editor (TextWrangler) and type some stuff into a new file. Save that file to your Desktop and name it ex12.txt. In your shell use the commands you know to copy this file to your temp directory that you've been working with.
Working from parent directory that contains both the desktop and the folder I want to copy the text file into.
cp desktop/ex12.txt temp/ (first specify path to text file then specify folder to py in)
cd temp
ls (confirm you have moved the file)

less ex12.txt (displays content in text file in fresh command window, type q to exit out)
more ex12.txt (displays content ot text file in the same window, use spacebar+w to move through pages if the text file is long, type q to exit out)

Stream a file (cat filename)

cat ex12.txt (displays text file content on same window, no need to press q to continue)
cat ex12.txt ex13.txt (displays both text files contents on same window, no need to press q to continue)

Removing a file (rmdir filename)

rmdir sweet.txt (remove file called sweet)
rmdir dirty.txt iamcool.txt (remove two files called dirty and iamcool)

ls something (view the contents in the folder ls)
awesome.txt hot.txt
cd .. (back out into the folder containing the folder something)
cp -r something newplace (copy content in the folder called something into a folder called newplace)
rm something/awesome.txt (remove the a file from the folder something)
rm something/hot.txt
rmdir something (remove the now empty folder called something)
rm -rf newplace (shortcut to deleting a non empty folder together with its contents)

Pipes and redirection

The | takes the output from the command on the left, and "pipes" it to the command on the right. E.g.
cat ex12.txt ex13.txt | less

The < will take and send the input from the file on the right to the program on the left. E.g.
cat < ex13.txt
less < ex12.txt
less < ex12.txt | cat | less

The > takes the output of the command on the left, then writes it to the file on the right. You see me do that on line 9.
E.g.
cat ex13.txt > ex15.txt
cat ex15.txt

The >> takes the output of the command on the left, then appends it to the file on the right.

Looking inside files (grep)

First create a new file
cat > name_of_file
E.g.
cat > oldfile.txt

Now type in your text. Press the Return key to start a new line.
E.g.
this is old file
this is old file

When you have finished typing in your text, enter Ctrl-d (Press and hold down the Ctrl key and type a "d"). This stops the cat command and returns you to the system prompt.

grep new *.txt (searches for the word new in the text files within the folder)
newfile.txt:this is a new file
newfile.txt:this is a new file
newfile.txt:this is a new file somefile.txt:cat > newfile.txt somefile.txt:less newfile.txt

grep old *.txt
_oldfile.txt:this is old file _ oldfile.txt:this is old file

cat > oldnew.txt
this is old newfile

grep old *.txt (search for the word old in the text files)
_oldfile.txt:this is old file _ _oldfile.txt:this is old file _ _oldnew.txt:this is old newfile _

grep file *.txt (search for the word file in the text files)
_newfile.txt:this is a new file _ _newfile.txt:this is a new file _ _newfile.txt:this is a new file _ _oldfile.txt:this is old file _ _oldfile.txt:this is old file _ _oldnew.txt:this is old newfile _ _somefile.txt:cat > newfile.txt _ somefile.txt:less newfile.txt

grep "new file" *.txt (use qoutes to look for spaced words)
grep **N**ew *.txt (returns nothing because of the mismatched case)
grep -i new *.txt (ensures that the command is case insensitive)

Getting the command help (man)

This will display a manual for any command you question

man grep (returns manual for the grep command)
man less
man man
man cat

Finding help (apropos)

Sometimes you forget the name of a command but you know what it does. This command looks through all the help files and finds potentially relevant help for you.
E.g.
apropos search
apropos print
apropos remove

What is in your environment

Env (dumps my entire environment on the sreen)
echo $USER (dumps my environment pertaining to my username only)
jjudan7
echo $PWD
/Users/jjudan7
export TESTING="1 2 3"
echo $TESTING
1 2 3
env | grep TESTING
TESTING=1 2 3

Changing environment variables

export TESTING="ba ba ba"
echo $TESTING
ba ba ba
unset TESTING
echo $TESTING(returns blank since unset cleared the environment)
env | grep TESTING (returns blank since unset cleared the environment)

Exit my terminal

exit

Other commands

CHMOD
chmod - change file modes or Access Control Lists

SYNOPSIS chmod [-fv] [-R [-H | -L | -P]] mode file ... chmod [-fv] [-R [-H | -L | -P]] [-a | +a | =a] ACE file ... chmod [-fhv] [-R [-H | -L | -P]] [-E] file ... chmod [-fhv] [-R [-H | -L | -P]] [-C] file ... chmod [-fhv] [-R [-H | -L | -P]] [-N] file ...

DESCRIPTION The chmod utility modifies the file mode bits of the listed files specified by the mode operand. It may also be used to modify the Access Control Lists (ACLs) associated with the listed files.

 The generic options are as follows:

 -f      Do not display a diagnostic message if chmod could not modify the
         mode for file.

SUDO sudo - execute a command as another user

SYNOPSIS sudo -h | -K | -k | -L | -V

   sudo -v [-AknS] [-g group name|#gid] [-p prompt] [-u username|#uid]

   sudo -l[l] [-AknS] [-g group name|#gid] [-p prompt] [-U user name]
   [-u user name|#uid] [command]

   sudo [-AbEHnPS] [-C fd] [-g group name|#gid] [-p prompt]
   [-u user name|#uid] [VAR=value] [-i | -s] [command]

   sudoedit [-AnS] [-C fd] [-g group name|#gid] [-p prompt]
   [-u user name|#uid] file ...

DESCRIPTION sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. The real and effective uid and gid are set to match those of the target user as specified in

XARGS NAME xargs -- construct argument list(s) and execute utility

SYNOPSIS xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr] [-L number] [-n number [-x]] [-P maxprocs] [-s size] [utility [argument ...]]

DESCRIPTION The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments.

 Any arguments specified on the command line are given to utility upon
 each invocation, followed by some number of the arguments read from the
 standard input of xargs.  The utility is repeatedly executed until stan-
 dard input is exhausted.

 Spaces, tabs and newlines may be embedded in arguments using single
 (`` ' '') or double (``"'') quotes or backslashes (``\'').  Single quotes

CHOWN chown -- change file owner and group

SYNOPSIS chown [-fhv] [-R [-H | -L | -P]] owner[:group] file ... chown [-fhv] [-R [-H | -L | -P]] :group file ...

DESCRIPTION The chown utility changes the user ID and/or the group ID of the speci- fied files. Symbolic links named by arguments are silently left unchanged unless -h is used.

 The options are as follows:

 -f      Don't report any failure to change file owner or group, nor mod-
         ify the exit status to reflect such failures.

 -H      If the -R option is specified, symbolic links on the command line
         are followed.  (Symbolic links encountered in the tree traversal
         are not followed.)

OTHER SOURCES

[http://cli.learncodethehardway.org/bash_cheat_sheet.pdf]

[http://www.gnu.org/software/bash/manual/bashref.html]