LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-04-2022, 08:44 AM   #1
DynV
Member
 
Registered: Feb 2020
Location: Montreal, Canada
Distribution: Ubuntu 22.04.2
Posts: 51

Rep: Reputation: 1
Only keep filename (crop directories)


There's a script I didn't make that does
Code:
printf 'to copy: %s\n' "$src"
and $src is the full path. What's the way to output only the filename (thus crop directories (ie crop the last '/' and everything before)) that take the least processing, or close to?

Thank you kindly
 
Old 07-04-2022, 08:52 AM   #2
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
basename is the command you want.
 
Old 07-04-2022, 09:11 AM   #3
DynV
Member
 
Registered: Feb 2020
Location: Montreal, Canada
Distribution: Ubuntu 22.04.2
Posts: 51

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jmgibson1981 View Post
basename is the command you want.
The script does more than only what's in the OP and I assume if I did basename instead of however the file names are currently fetch (full path) that it would break the script.
 
Old 07-04-2022, 09:16 AM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
You will need to post more than a single line of a script for help more specific.
 
Old 07-04-2022, 09:19 AM   #5
DynV
Member
 
Registered: Feb 2020
Location: Montreal, Canada
Distribution: Ubuntu 22.04.2
Posts: 51

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jmgibson1981 View Post
more than a single line of a script
Code:
#!/bin/bash -

if (($# < 2)); then
  printf 'usage: %s SOURCE_DIR DEST_DIR\n' "$9"
  exit 1
fi

shopt -s nullglob dotglob globstar

src_dir=$1 dst_dir=$2

for src in "$src_dir"/**/*; do
    [[ ! -L $src && -f $src ]] \
        || continue
    dst=$dst_dir/${src#"$src_dir"/}

    if
        [[ ! -e $dst ]] || {
            [[ -f $dst ]] &&
            src_size=$(stat -c %s -- "$src") &&
            dst_size=$(stat -c %s -- "$dst") &&
            [ "$src_size" -gt "$dst_size" ]
        }
    then
        printf 'to copy: %s\n' "$src"
        cp -- "$src" "$dst"
    fi
done
 
Old 07-04-2022, 10:35 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
Code:
${src#"$src_dir"/}
This chops from the left end.
It chops the leading common directory (start directory).
The following chops from the left end, and lets the * expand to the maximum (## not # where * expands to the minimum).
Code:
${src##*/}
 
1 members found this post helpful.
Old 07-04-2022, 09:49 PM   #7
DynV
Member
 
Registered: Feb 2020
Location: Montreal, Canada
Distribution: Ubuntu 22.04.2
Posts: 51

Original Poster
Rep: Reputation: 1
Following your clue, I did a small change doing what I want which I highlighted in red what's moved and in green what's added/modified:
Code:
#!/bin/bash -

if (($# < 2)); then
  printf 'usage: %s SOURCE_DIR DEST_DIR\n' "$9"
  exit 1
fi

shopt -s nullglob dotglob globstar

src_dir=$1 dst_dir=$2

for src in "$src_dir"/**/*; do
    [[ ! -L $src && -f $src ]] \
        || continue
    src_name=${src#"$src_dir"/}
    dst=$dst_dir/$src_name

    if
        [[ ! -e $dst ]] || {
            [[ -f $dst ]] &&
            src_size=$(stat -c %s -- "$src") &&
            dst_size=$(stat -c %s -- "$dst") &&
            [ "$src_size" -gt "$dst_size" ]
        }
    then
        printf 'to copy: %s\n' "$src_name"
        cp -- "$src" "$dst"
    fi
done

Last edited by DynV; 07-04-2022 at 09:55 PM.
 
Old 07-05-2022, 03:15 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
You have shortened the printout,
and kept the full path for the cp.
Well done!
 
Old 07-05-2022, 06:43 PM   #9
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,681
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by MadeInGermany View Post
You have shortened the printout,
and kept the full path for the cp.
Well done!
if that is what DynV wanted, then he should be happy. he didn't make it clear to me.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Only commercial tools can crop PDFs? libCog Linux - Software 2 12-01-2012 02:32 PM
[SOLVED] Mencoder yields 2GB output without crop, 2MB output with crop porphyry5 Linux - Software 2 01-11-2012 06:44 AM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration