Results 1 to 7 of 7

Thread: Gow do you cd into a directory with multiple word name?

  1. #1
    Join Date
    Jan 2008
    Location
    Davison, Michigan, U.S.A.
    Beans
    1,128

    Gow do you cd into a directory with multiple word name?

    File is in /Home/Username/ Documents/Folder Containing PDF Files/names.pdf
    I want to grep the names.pdf (I'm new to grepping). I can cd to 'Documents' but how do I cd to 'Folder Containing PDF Files'

  2. #2
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,263
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Gow do you cd into a directory with multiple word name?

    Use quotes round the filename, or use a backslash in front of the spaces. I guess you mean /home (Linux is case sensitive), and there is not really a space before Documents.
    Code:
    cd "/home/Username/Documents/Folder Containing PDF Files/names.pdf"
    # or
    cd '/home/Username/Documents/Folder Containing PDF Files/names.pdf'
    # or 
    cd /home/Username/Documents/Folder\ Containing\ PDF\ Files/names.pdf'
    Note that
    You can use "tab completion" to finish partially entered filenames which helps a lot (type the start of the name then press the tab key). This will either finish the filename or stop and wait where there is more than one possible next letter. So this will probably get you there:
    Code:
    cd /ho<TAB>Username/Doc<tab>Fold<tab>names<tab>

  3. #3
    Join Date
    Jul 2005
    Location
    I think I'm here! Maybe?
    Beans
    Hidden!
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Gow do you cd into a directory with multiple word name?

    I always replace spaces in a file or folder name with an underscore to avoid so many of these problems.

    In my Xubuntu I have a custom actions item to do this with a right click using command
    Code:
    rename 's/\ /_/g' %F
    Dead simple, very quick, and incredibly useful.

  4. #4
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,386

    Re: Gow do you cd into a directory with multiple word name?

    As shown above already, quotes and tab are your friend.

  5. #5
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Gow do you cd into a directory with multiple word name?

    In addition to using tab-completion, I will use wildcard patterns in shells.
    BTW, grep doesn't really work on PDF files. Need to convert those to text .... pdftotext is the tool for that. PDF files are encoded using ASCII-85 and can be compressed, making text search tools not so good.

    So, if I wanted to look inside this (I think it has typos with an extra space BEFORE "Documents" and /Home is also wrong, but whatever, it is what you provided, assuming that is all correct)
    /Home/Username/ Documents/Folder Containing PDF Files/names.pdf
    I'd use
    Code:
    $ pdftotext /Hom*/User*/*Doc*/Fol*/names.pdf - | egrep {some kewl regex}
    If I correct it for the bad typos above, I'd use
    Code:
    $ pdftotext ~User*/Doc*/Fol*/names.pdf - | egrep {some kewl regex}
    Let me go test that. Yep, the trick is that pdftotext only accepts a single input file, so to search multiple files at the same time, we'd need to wrap the pdftotext use in a loop ... I typically do that like this:
    Code:
    #!/bin/bash
    for filename in "$@"; do
       echo "INFO: Searching on $filename
       $(/usr/bin/pdftotext  $filename - | egrep -i {some kewl regex})
    done
    To use this script, say it is ~/bin/pdfgrep, then
    Code:
    $ ~/bin/pdfgrep ~User*/Doc*/Fol*/*.pdf
    All sorts of caveats exist doing this. Input filenames cannot have spaces because spaces are the default delineation character for "$@" expansion. There are ways to handle that, but they are all a hassle. As ajgreeny & Herman say, removing all spaces from directory and filenames is step one to a happier life using scripts. I fought this for a few years, before giving up and just removing any funky characters from all filenames. That was over 20 yrs ago and I've been so much happier in my scripting since doing that.
    Last edited by TheFu; May 24th, 2021 at 04:04 PM.

  6. #6
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Gow do you cd into a directory with multiple word name?

    RipGrep searches also PDF .. and other types ...

    https://github.com/phiresky/ripgrep-all

  7. #7
    Join Date
    Jan 2008
    Location
    Davison, Michigan, U.S.A.
    Beans
    1,128

    Re: Gow do you cd into a directory with multiple word name?

    Wow! You guys are like some kind of space alien geniuses from the future. It took me about an hour to figure this out. I settled on the quotation mark method. I was very impressed with the tab method. I used both. I was able to convert the pdf into text and save it. I then grepped the text file and scrolled through it forward and backward. Now I can't remember why I wanted to do this. I'm sure it will come to me eventially. Thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •