I wanted to delete all .sh extensions so did this:
ls *.sh | xargs -I {} mv {} `basename {} .sh`However it doesn't work, it behaves like basename returns unchanged file name.
Why is it behaving that way ?
For instance, this works:
ls *.sh | xargs -I {} echo `basename {}.jpg .jpg`;EDIT:
Solution: single quote prevents `basename ...` evaluation by the shell before the command is run.
ls *.sh | xargs -I {} sh -c 'mv {} `basename {} .sh`'asked Feb 18, 2013 at 18:37
majkinetormajkinetor
5421 gold badge5 silver badges17 bronze badges
Because the basename command is run before the pipeline is run. To make this work you need xargs to execute basename and you can do that with sh -c, e.g.:
ls *.sh | xargs -L1 sh -c 'basename $1 .sh' dummyNotes:
- If you don't tell xargs where to insert the file names they will be added at the end of the command line.
- You should use the -L1 switch or its equivalent, so xargs only passes one argument to sh.
- Using the output of ls may have unwanted effects.
Edit
Removed deprecated options, thanks TechZilla
answered Feb 18, 2013 at 20:10
ThorThor
16.2k3 gold badges51 silver badges68 bronze badges
9
I'm unsure about your question, are you actually asking why your first line doesn't work? Or are you looking for a proper method to rename all the .sh files?
Assuming it's the cleanest method, I prefer to prepare command options before xargs. For removing a .sh file extension, I often consider this approach,
find -maxdepth 1 -type f -iname '*.sh' | sed 'p;s_.sh$__' | xargs -L2 mvanswered Feb 18, 2013 at 20:10
J. M. BeckerJ. M. Becker
4,6213 gold badges26 silver badges40 bronze badges
Not the answer you're looking for? Browse other questions tagged bash text-processing xargs or ask your own question.
xargs builds the command from the standard input - however, it is not available for Windows command shell... therefore I 've decided to build one and it turns out a Batch script is enough...
Source: //github.com/DoctorLai/BatchUtils/blob/master/xargs.cmd
@echo off :: example: git branch | grep -v "develop" | xargs git branch -D :: example xargs -a input.txt echo :: //helloacm.com/simple-xargs-batch-implementation-for-windows/ setlocal enabledelayedexpansion set args= set file='more' :: read from file if "%1" == "-a" ( if "%2" == "" ( echo Correct Usage: %0 -a Input.txt command goto end ) set file=%2 shift shift goto start ) :: read from stdin set args=%1 shift :start if [%1] == [] goto start1 set args=%args% %1 shift goto start :start1 for /F "tokens=*" %%a in (!file!) do ( %args% %%a ) :endBuilding command from standard input:
Building command from text file:
Reposted to blog: //helloacm.com/simple-xargs-batch-implementation-for-windows/
Enjoy and Steem On!
Delegate to @justyy
@justyy runs a automatic delegation service for nearly a year now. Delegate to @justyy for at least 5 SP and start receiving daily payout as interests (from 8% to 10% APR). Also, as a supporter, the delegators will start to receive complimentary/curation upvotes (as a thank you) per day from e.g @justyy and a few other curation trails. For more information, read this. The voting weight algorithm is open source.
- Delegate to @justyy - at least 5 SP to join (automatically)
- Undelegate to @justyy (Quit) - SP returned by steem blockchain in 5 days
- View Current Delegators/Supporters
Please note that the SP you enter is the final amount to delegate. For example, if you already delegate 10 SP and you want to delegate another 5 SP, you will need to enter 15 SP (instead of 5 SP) in the delegation form.
Vote for me or Set me as a witness Proxy - Every vote counts! - Thank you!
Your Vote is much appreciated, and every vote counts.
Check out My Witness Page
Support me and my work as a witness - witness thread by
- voting me here, or
- voting me as a witness proxy - let @justyy represent you.
Thank you! Some of My Contributions: SteemYY.com - SteemIt Tutorials, Robots, Tools and APIs and VPS Search Tool