LinUnix Commands

Discussion in '21C Reference Documents' started by Dan Allen, Sep 21, 2015.

  1. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    If you have any questions, please contact Michelle, Kirsten or Dan.

    host (find ip address and email for a web domain)

    mkdir (make directory)

    cd (change directory) (cd ~ to get to home directory)

    ls (list)

    touch (make file)

    rm (remove—careful this is irreversible)

    rm - r (remove a directory)

    -a (dash is the ‘option’, ‘a’ is the argument) means list hidden system files

    cd .. (move back up one level in the file system)

    cd/users/everonthego/Desktop (type out full path)

    cd .. / (relative path command when both files share a parent directory) no space between /file

    pwd (show present working directory)

    -i (toggles interactive mode to confirm deletion or other action)

    cd ~ (return to User folder)

    mv (move, then enter path to new location, can also be used to rename a file)

    man (show usage options for a command, example: man ls, type ‘q’ when finished)

    To abort a command: ‘control c’ (looks like ^c)
    I also picked up a few tutorials and tips along the way. This, about Linux (which I believe is similar?)

    Important facts about file names
    1. File names that begin with a period character are hidden. This only means that ls will not list them unless you say ls -a. When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of these files to see how you can customize your environment. In addition, some applications will place their configuration and settings files in your home directory as hidden files.


    2. File names in Linux, like Unix, are case sensitive. The file names "File1" and "file1" refer to different files.


    3. Linux has no concept of a "file extension" like legacy operating systems. You may name files any way you like. However, while Linux itself does not care about file extensions, many application programs do.


    4. Though Linux supports long file names which may contain embedded spaces and punctuation characters, limit the punctuation characters to period, dash, and underscore. Most importantly, do not embed spaces in file names. If you want to represent spaces between words in a file name, use underscore characters. You will thank yourself later.
    A tutorial that I found via TigerTech and is useful: (Linux, but still on the bash shell) Learning The Shell

    A beginner tutorial by MacTuts: Navigating the Terminal: A Gentle Introduction

    A YouTube tutorial: (goes by fast but has a lot of stuff, no part two, even though it says there is one): Terminal Tutorial Part 1: Commands and Concepts

    We also had a conversation about the ll command early in the programming internship. It's here in the Programming Internship BaseCamp: Enable the ll command

    Logging in as root was covered there as well: Enable the Root User

    the general syntax of command line statements is
    commandname -optionabbreviation optionvalue -asmanyoptionabbrevisationsasyouwant -optionvalue --optionwholename optionvalue objectsToCommandUpon

    It gets complicated.

    example
    zip -r zipfile.zip --exclude pathToExclude pathToFilesDirectoriesToInclude

    Kirsten Bolda said:
    -i (toggles interactive mode to confirm deletion or other action)
    The letters of options vary by command. -i can mean different things for different commands, or if ti doesn't that would be a miracle.

    Kirsten Bolda said:
    cd ~ (return to User folder)
    usually referred to as home directory.

    If you want to represent spaces between words in a file name, use underscore characters. You will thank yourself later.
    Better advice never given.

    ADD:
    Text editing on unix.
    Take a look at the two files attached*, textWindow.txt and textUnix.txt. Open the files. Do you see the difference? Answer: difference is not visible, but one file works when it is run on unix and other doesn't Why?
    * I need to provide the files

    How text edit: use nano or vi.


    lists directory and file sizes in order of size
    du -m -h -s -c -- * | sort -h ;

    find a file
    find . -iname 'filename' (can use * as wildcard)


    find occurrences of a string files
    grep -H -r 'stringtofind' pathtosearch


    Search files for string, and replace the string with another
    find . -type f -exec sed -i 's/stringtofind/string to put when otherone is found /g' {} +
    warning: if string includes $, / or just about anything other than numbers and letters, this gets absurdly tricky.

    dump a database
    mysqldump -u username -p databasename > databasename.sql

    To load a database from a dump
    mysql -u username -p databasename < dumpfile

    On plesk:
    instead of -u username -p, use
    -u admin -p`cat /etc/psa/.psa.shadow` it always works. This helpful


    Pull files from another server
    wget ftp://ftpaccountname:ftppassword@ftpaddressofotherservr/path-to-pull

    wget can be made recursive, but using -m but instead of doing that, it is better to zip all the stuff all files and directories you need to pull, then pull the one file, unzip. Much faster and simpler

    Check how much disk is used and available
    df

    setting up aliases (I need to elaborate)
    setting up symbolic links (will elaborate in conjunction with aliases)
     
  2. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    A very useful command: cat filename

    (where filename is the name of the file in question)

    Use cd directoryname (change directory)
    And cd .. (move back up one directory)
    And ll (two lower case letter Ls, list contents of directory)
    And cat filename

    To move around the entire server and examine its contents.

    For more on this command: (link)

    upload_2016-1-9_12-54-29.png

    Example of this command in action:
    upload_2016-1-9_12-58-41.png
     
  3. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    I love seeing find all these things.
     
  4. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    What a great way to locate a file, or even a particular expression, inside a directory. In my case, I was in search of a function inside the clone of Holly's system.
    The command to get that done is called grep, and the syntax goes like this:

    grep (the command) -H (causes the file name to be printed) r (does a recursive search) then 'put the string for the file in question here' . (the period starts the search at the file you are in )

    For my function search I first listed (ll) and changed directories (cd) until I was in the cloned system directory, then entered:

    grep -Hr 'function add_dl_links' .

    It returned:
    ./pageparts/000-common/001-dansoft-emp.php:function add_dl_links($page_content){ //return $page_content;
    ./zDansoft/pageparts/000-common/001-dansoft-emp.php:function add_dl_links($page_content){ //return $page_content;
    ./0000000-archive/howtothinksideways.com/20080414/pageparts/000-common/001-dansoft-emp.php:function add_dl_links($page_content){ //return $page_content;


    So that went well. :D

    upload_2016-3-5_15-14-21.png

    Some problems to look out for:
    1. Zip files or large .sql files in the search zone will fill the screen with useless information. There are ways to exclude those, but they are a lot of trouble to set up.
    2. Apostrophes and other characters that have to be escaped in the search string will make the search process more difficult to set up
     
    Dan Allen likes this.
  5. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    clear (clears all previous commands and files out of the window)

    unzip example (uncompresses the file, it is not necessary to include the .zip extension of the file. Also can be used to unzip a file on your computer. Just drag and drop the file you want to unzip into the command line window after entering unzip, then hit enter)
     
  6. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    cat can fill your screen with craziness but it does not run programs, making it a safe stick for poking at things.
     
  7. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    dirsize (Lists all the files, then directories in the present working directory, followed by the size and number of files in the directory)

    upload_2016-12-8_15-32-49.png
     
  8. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    On OSX, try this to see super clear:

    cat /var/log/auth.log

    Lots of stuff comes onto screen.

    clear

    now try scrolling up. So much stuff!!! what if you want run a command and have nothing in terminal window except the output of that command?

    Click on the edit menu and select "clear to start"

    Now try scrolling up... no stuff!! niiiiiiiiiiiiiiiiiiiiiiiiiiiiiice.

    That is super clear on a MAC. Other platforms I don't know what they do, but my heart goes out to them.
     
  9. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    dirsize is a shell script I put into your ~/bin directory. You can see it by

    cd ~/bin
    ll

    The reason it runs no matter which directory you are in when you type is that these lines are in your ~/.bashrc file at the end

    PATH=$PATH:$HOME/bin:~/bin/:
    export PATH
     
  10. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    cp (copy)

    Example:
    • Navigate to source directory upload_2016-12-8_16-20-26.png
    • Check dirsize:
    • upload_2016-12-8_16-26-55.png
    • Enter cp (copy) * (everything in the present directory) ../exampledirectory (target directory, in this case a sibling directory, one step up the tree and back down) upload_2016-12-8_16-19-26.png
    • Test by navigating to target directory and checking dirsize against source directory dirsize upload_2016-12-8_16-24-57.png
     
  11. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    I see there are some of the other commands you made in there too:
    upload_2016-12-8_17-18-40.png

    That's pretty cool. :cool:

    OMG! It just never stops. It's like I'm in the Matrix!
    Ah, finally done.
    Just a section of the whole thing:

    upload_2016-12-8_17-28-52.png

    I couldn't get the result you had though. I could clear it all, but not with this:
    upload_2016-12-8_17-36-1.png

    'Clear scrollback' seemed to lock up Terminal, and Clear took me back to a completely clear screen.
     
  12. Dan Allen

    Dan Allen Administrator Founder Not Banned Radio Button Problem - Leader

    another sample of the same deal.
    upload_2016-12-9_8-32-33.png


    upload_2016-12-9_7-55-36.png

    This file contains a video (2:29 in duration) of cat -n rsync_mondoboss.log running on my screen, plus the file rsync_mondoboss.log. I picked the number 427,000 out of the air and looked to see what is there and if that is a stable result.

    If you get a chance to take in this video, just for kicks, can you cat the file to your screen and confirm that line 628734 contains this string exactly?
    2016/10/31 09:45:49 [6664] >f+++++++ www.devserver-002.info/htdocs/testareas/array/css/random.gradients.all.rights.reserved.css

    If you find that string, does it have a space at the end of it?

    I just think it is so amazing that we have this level of precision at our disposal on this scale
     
  13. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    I'm confused about the 'cp' command because I am able to copy a file from one directory to another, but when I try to do the same with with a directory the Terminal will sit and think about it for a moment, but then respond that since this is a directory it is not copied.

    upload_2017-1-29_9-54-55.png

    upload_2017-1-29_10-6-29.png

    Everything I look up about Terminal and Linux/Unix (Example: cp command in Linux/Unix @ Rapid Tables
    upload_2017-1-29_10-24-51.png

    ) indicates that copying a directory is no different than copying a file, but apparently that is not the case, because the command:
    Code:
    cp monstra-3.0.4 ../Practice-cp
    generates the complaint that a directory will not be copied.
    I can't seem to find anything that shows me what I am doing wrong, even though I bet it's obvious to anyone experienced with this stuff.
    The reason I am doing this exercise is so that I can do some rearrangement of these directories on my server. Moving them around with FileZilla is cumbersome and inefficient, and besides copying directories seems like a good skill to learn. :)
     
    Last edited: Jan 29, 2017
  14. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    Here's a shortcut I just learned that is saving me a bunch of typing and even copying and pasting.
    When you want to change directory, instead of typing out the whole directory name, or copy/pasting it in, just hit the first letter (or first few depending on whether other directories share the first initials) and then the tab key.
    The command line will fill in the rest of the name in the directory up to the next non unique character. An example makes this easier to explain.

    Let's say I'm at kbdc:

    upload_2017-2-15_20-2-50.png

    And I want to head on over to the dbadminxCCdFyeHFYZJGReE/ directory. Sure, I can copy/paste that bad boy into the command line but, how about I just type cd dba and hit tab to let the computer do it for me?

    upload_2017-2-15_20-6-19.png

    upload_2017-2-15_20-7-58.png

    (edited to add: technically I could just have typed 'db' and that would be enough for Terminal to fill in the rest of the directory name.)
     
    Dan Allen likes this.
  15. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    Changing a password:

    Once you are logged in, type:

    Code:
    passwd
    You will be prompted for your new password:

    [​IMG]
     
  16. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    Moving directories:
    from this post: https://legacy-systems.biz/index.php?threads/i-think-you-will-want-to-see-this.46/page-2#post-274
    Code version of mv:
    Code:
    mv directoryName path/to/new/directoryName
    Moving files is accomplished in the same way as moving a directory.
    Reference: https://legacy-systems.biz/index.php?threads/i-think-you-will-want-to-see-this.46/page-3#post-290
     
  17. Kirsten Bolda

    Kirsten Bolda Administrator Radio Button Problem - 2nd Demo Parts A and B Founder Not Banned

    Sixty core Bash commands all in one place, with explanations and examples for each:

    60 Bash Commands
     

Share This Page