Bash one-liners

Discussion in 'all things UNIX' started by vasa1, Jan 31, 2012.

Thread Status:
Not open for further replies.
  1. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
  2. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    That has two steps (both of which I don't understand at all)
    Code:
    $ file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:
    
    cp $(file /proc//fd/ 2>/dev/null | grep Flash | cut -f1 -d: | head -n 1) video.avi
    While the first seems to work ... because no error shows up and the prompt returns, the second gives this "error":
    Code:
    cp: missing destination file operand after `video.avi'
    Try `cp --help' for more information.
    
    Any ideas?
     
  3. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,677
    Location:
    George, S.Africa
    You are obviously keen on drilling right down to the nitty gritty, commendable of course, but why not just fish the video files from browser cache ? Eg. with Opera find YouTube in the list and look for the video playback files, then just save to download folder. Sure the extension is not shown in list but in this example it will be saved as videoplayback.flv
    I get all my classical music videos (usually 25 mins. HD) like that and rename them with mp4 extension.
    Hope this isn't too off topic o_O

    Opera cache.png
     
  4. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    Well, actually, I use "Download YouTube Videos as MP4 and FLV1.3.5" with Firefox. It puts a neat button below the video and downloads the video. It's just that I came across that cp stuff and was curious! I had the impression that Firefox had started chopping files in cache to less than 5 MB and that had raised a wee storm when it first became known. People who used to fish out stuff from cache were not amused.

    Also, I wanted to know what "missing destination file operand after b" could mean in the context of cp a b. Since I don't know what's going on, I thought that "$(file /proc//fd/ 2>/dev/null | grep Flash | cut -f1 -d: | head -n 1)" was somehow the first file, a. And that "video.avi" would be the second file, b.
     
  5. Searching_ _ _

    Searching_ _ _ Registered Member

    Joined:
    Jan 2, 2008
    Posts:
    1,988
    Location:
    iAnywhere
    @vasa1
    Did you put each line in at the terminal prompt or did you put it all into a text file with a hashbang header?
     
  6. iceni60

    iceni60 ( ^o^)

    Joined:
    Jun 29, 2004
    Posts:
    5,116
  7. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    I entered
    Code:
    $ file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:
    Then hit enter and got the prompt.
    Then I entered
    Code:
    cp $(file /proc//fd/ 2>/dev/null | grep Flash | cut -f1 -d: | head -n 1) video.avi
    and hit enter and got
    Code:
    cp: missing destination file operand after `video.avi'
    Try `cp --help' for more information.
    I gave the link to where it got the stuff from but here it is again: http://www.bashoneliners.com/main/oneliner/17/
     
  8. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    That first link certainly looks interesting. Thanks! I will check it out tomorrow. It's bedtime here!

    Re. splittng cache files if they exceed 5 MB, I saw that over at the mozillazine forum within the last year. I'll dig out that link tomorrow. Plus, I haven't come across any mention of a "roll-back" of that decision.
     
  9. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    Okay, this was easy and you seem to have got it right:
    https://bugzilla.mozilla.org/show_bug.cgi?id=647391 (resolved fixed)
    Comment #19:
    and the old stuff I was referring to: http://forums.mozillazine.org/viewtopic.php?f=38&t=2141531
     
  10. Searching_ _ _

    Searching_ _ _ Registered Member

    Joined:
    Jan 2, 2008
    Posts:
    1,988
    Location:
    iAnywhere
    @vasa1

    Open a text editor and create a file called shebang_vasa1.sh :D
    Copy this into it:
    Code:
    #!/bin/bash
    $ file /proc/*/fd/* 2>/dev/null | grep Flash | cut -f1 -d:
    Next cd to the location of shebang_vasa1.sh and make executable.
    Code:
    $ chmod +x shebang_vasa1.sh
    Now you can execute your bash script
    Code:
    ./shebang_vasa1.sh
    Modified advice from this Bash Scripting Tutorial.
     
  11. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    @iceni60 and @Searching_ _ _, thanks for your help but I think I've to evolve a bit more before I can figure out these things.
    (I have gotten apt-fast to work though and that's a great help for those of us with slow networks.)
     
  12. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    Well, I got something similar. I use find to list files larger than 2 MB from cache:
    Code:
    #! /bin/bash
    find /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache -type f -size +2M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'
    and then copy the appropriate ones, all videos so far, to wherever I want. (I even understand most of what the script does ;) )
    Later, I use Audacity to keep just the sound.

    But I've officially given up on trying to get things out of Chrome.
     
  13. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    This was my old code:
    Code:
    #! /bin/bash
    find /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache -type f -size +2M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'
    
    The disadvantage is that it picked up files like "_CACHE_003_", "_CACHE_001_", "_CACHE_002_", and "_CACHE_MAP_" which aren't what I want. These files grow with time and get picked up with +2M. I could change +2M but that's not the point.
    So now I'm trying
    It starts lower down and doesn't pick up the unwanted files.

    My output looks like this (bringing the size down to +300k just to get more hits but not shown above):
    Code:
    1.5M  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/0/A1/1831Ed01
    1.7M  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/2/B5/29B02d01
    301K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/3/23/86D18d01
    301K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/4/31/8A192d01
    301K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/6/00/21354d01
    370K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/7/BB/7EBF1d01
    468K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/E/B1/44F40d01
    407K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/E/5D/518A1d01
    301K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/F/3B/6AFD5d01
    404K  : /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/F/CC/86959d01
    
    My latest itch is to have the output sorted by size but I don't know how. Changing ls -lh to ls -lhS works when I use it directly as a command but doesn't seem to work in the -exec context of the find command.
     
  14. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    This does the sorting:
     
  15. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    This is my latest effort:
    Code:
    find /home/vasa1/.mozilla/firefox/3sw4w8a1.default/Cache/*/ -type f -size +2M -printf "%p\n" | tee ~/Desktop/list.txt && xargs -a ~/Desktop/list.txt mv -t /home/vasa1/Desktop/ex-cache
    It finds files (that exceed a specified size) in the various cache subdirectories, prints out a list of just the full filenames, one per line, both to screen and to a file. This file is then used to move the listed files from their old location to a new one.
    Note that if there aren't any files of the specified size, mv will throw up this very informative comment:
    Code:
    mv: missing file operand
    Try `mv --help' for more information.
    
    AFAICT, moving files out of the cache this way hasn't caused Firefox 11 (beta) to explode (as yet).
     
  16. linuxforall

    linuxforall Registered Member

    Joined:
    Feb 6, 2010
    Posts:
    2,136
    Clipgrab which has its own ppa for Ubuntu is the best grabber and converter for youtube and other video sites.
     
  17. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    The thing is I get to know about stuff which I might not learn if I went the ppa route ... Another point, is that I'd like to keep ppas to a minimum. Just like I don't load up on extensions for browsers.
     
  18. x942

    x942 Guest

    Check speed of hdd:

    Code:
    time (dd if=/dev/zero of=zerofile bs=1M count=500;sync);rm zerofile
    Find all unique 4 letter words in a text:

    Code:
    cat document_here.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}
    Running applications over X11 forwarding via SSH. (This is a great way to run linux tools on a non-linux box like windows).

    Code:
    ssh -X servername/ip address
    Can't remember if this one was posted but this executes the previous command (!!) as root:

    Code:
    sudo !!
    Alert by e-mail when a disconnected or unreachable server comes back online:

    Code:
    while ! ping -c1 the_host_down; do sleep 1; done && date | mail -s 'the host is back!' me@example.com
     
  19. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,677
    Location:
    George, S.Africa
    Couple more >> (Have them saved in a txt file so as not to forget)

    !(word)

    Perhaps a few days ago you typed a long, complex command into your shell, for example a series of options for “wterm”. You can find and re-enter than command by using the history’s built in search. You simply add the first few letters of the command after the ! and bash will find it.

    !?(word)

    This is similar to the last feature in that it searches the history for the word entered, but unlike !(word) this will find the word anywhere in the command, not just the beginning.

    Ctrl-R

    This one may be my personal favorite history tool. It’s a bit like the !? item above, but interactive. In your command shell, hit Ctrl-R, and it will begin a search. As you type, bash will search the history and show you the results as you type. When it shows the command you want, simply hit enter and it will run that command. This can be safer than things like !? because you can see what the command will be before you run it, so you don’t have to guess or rely on memory.


    You can also specify the number of items the history command will show. To see the last 10 entries in the history, you could type
    history 10

    To see all history entries that contain a particular word, you can use grep to filter the results.

    Word replacement

    This one’s pretty great. How many times have you written out some big long command only to realize you put, say, hdd when you wanted hdc? Well bash has you covered. You can replace a word in the previous command with another using “^”, as in the example below.

    ~$ umane -a
    umane: command not found
    ~$ ^umane^uname^
    uname -a
    Linux ocky-desktop 2.6.32-38-generic #83-Ubuntu SMP Wed Jan 4 11:12:07 UTC 2012 x86_64 GNU/Linux
    ~$
     
  20. linuxforall

    linuxforall Registered Member

    Joined:
    Feb 6, 2010
    Posts:
    2,136
    For me the ppas help in easier deployment of Ubuntu across various users who would just need a functional up to date system. The PPA makes that possible.
     
  21. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,677
    Location:
    George, S.Africa
    Sometimes one simply needs certain ppas. For example for Handbrake and Kdenlive in my Kubuntu 11.10 64 bit. Handbrake has not yet made it into the repos and the Kdenlive version in the repos is buggy.
     
  22. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    No argument about that. That there's a ppa for something is no doubt useful information to know. I want to learn about using the CLI and a bit of scripting.
     
  23. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    Last edited: Feb 17, 2012
  24. vasa1

    vasa1 Registered Member

    Joined:
    May 1, 2010
    Posts:
    4,152
    My latest effort ;)
    Code:
    grep `date +"%Y-%m-%d"` /var/log/privoxy/logfile > ~/Desktop/plf
    writes entries having today's date in YYYY-MM-DD from logfile to plf
     
Loading...
Thread Status:
Not open for further replies.