LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-19-2009, 06:41 AM   #1
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
bash, find, xargs and built in commad 'cd'


setup:
------
Code:
mkdir -p $HOME/tmp/dir1
mkdir $HOME/tmp/dir2
this way we make sure theres exactly one dir called "dir2".

task:
-----
'find' dir2 and 'cd' into it by means of using xargs. this is what i did:
Code:
$ cd $HOME/tmp
$ find ./ -name dir2 -type d
./dir2
$ find ./ -name dir2 -type d | xargs echo cd
cd ./dir2
$ find ./ -name dir2 -type d | xargs cd
xargs: cd: No such file or directory
$ ls
dir1 dir2
$ cd ./dir2
$
any ideas?
 
Old 08-19-2009, 06:53 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Sorry -- can't reproduce; it works for me.

# find --version
GNU find version 4.2.32
# bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

What happens if you find . rather than find ./ (it shouldn't make any difference ...)

Is cd an alias? If so, what happens when you unalias it?
 
Old 08-19-2009, 08:04 AM   #3
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by catkin View Post
Sorry -- can't reproduce; it works for me.
hm,
Code:
bash-3.1$ bash --version
GNU bash, version 3.1.17(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
bash-3.1$ find --version
GNU find version 4.2.31
Built using GNU gnulib version 2007-02-24
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION 
bash-3.1$ xargs --version
GNU xargs version 4.2.31
Built using GNU gnulib version 2007-02-24
O_NOFOLLOW is enabled but this means only not to follow symbolic links as far as i know. LEAF_OPTIMISATION is no problem as well since linux filesystem layout is POSIX compatible. i don't know what D_TYPE means, though.

it's not aliased either.
Code:
bash-3.1$ alias
bash-3.1$
changing "./" to "." doesn't help. i reproduced it a thousand times . here is the exact output copied from the terminal (except for $HOME):
Code:
bash-3.1$ pwd
$HOME/tmp
bash-3.1$ ls
dir1  dir2
bash-3.1$ ls -R
.:
dir1  dir2

./dir1:

./dir2:
bash-3.1$ find ./ -name dir2 -type d 
./dir2
bash-3.1$ find ./ -name dir2 -type d | xargs echo cd
cd ./dir2
bash-3.1$ find ./ -name dir2 -type d | xargs cd
xargs: cd: No such file or directory
bash-3.1$
strange. :::

p.s. any slackware users that can reproduce?
 
Old 08-19-2009, 07:56 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
How about
Code:
find . -name dir2 -type d -exec cd {} \;

OR

find . -name dir2 -type d -print -exec cd {} \;
UNTESTED
 
Old 08-19-2009, 08:17 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Can confirm this to NOT be working on Slackware 12.2 and RHEL 5.3

Reason is that there's no stand-alone cd executable, which is what xargs (or
find's exec, for that matter) can't find.

Give xargs a miss, just do
cd $( find ... )



Cheers,
Tink
 
Old 08-20-2009, 04:01 AM   #6
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Tinkster View Post
Reason is that there's no stand-alone cd executable, which is what xargs (or find's exec, for that matter) can't find.
i though so. i was simply misinterpreting the error message "xargs: cd: No such file or directory". i somehow thought this is 'cd' complaining about a directory not found whereas it is 'xargs' complaining of not finding the command 'cd'. thanks for clearing up.

than ubuntu has a standalone 'cd' command? :::

p.s i was just trying something in this thread. it was not a practical problem. in an other thread i was wrongly saying that you could pipe to the cd command. this doesn't work so i figured with xargs it should be possible. then i came across this problem.

Last edited by :::; 08-20-2009 at 04:05 AM.
 
Old 08-21-2009, 08:04 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
I'm sorry -- I think I missed the xargs when testing; re-testing now I get the same behaviour. Embarassing but at least the sitation is simpler.

ubuntu 8.04.3 does not have a standalone cd. AIUI, no such thing could work -- it would cd in a sub-process and the PWD of the calling process would be unchanged.

Both "find" and "xargs" must create a sub-process run whatever command they are configured to run. That being the case, using "find" with "xargs" shouldn't make any difference. Presumably both simply fork and exec something to run the given command.

If they exec bash to interpret the command then "cd" is valid and should work; the PWD is presumably inherited from the shell that ran the initial "find". If they don't exec bash and simply ask the kernel to run the given command in the new sub-process then "cd" is not valid.

After the following experiments I conclude that both "find" and "xargs" simply ask the kernel to run the given command but "find" does not report any errors while xargs does.
Code:
c@CW8:~/d/bin/try$ /bin/ls -l not-a-dir 
/bin/ls: cannot access not-a-dir: No such file or directory
c@CW8:~/d/bin/try$ find ./ -name not-a-dir 
c@CW8:~/d/bin/try$ find ./ -name not-a-dir | xargs echo cd
cd
c@CW8:~/d/bin/try$ touch not-a-dir 
c@CW8:~/d/bin/try$ find ./ -name not-a-dir 
./not-a-dir
c@CW8:~/d/bin/try$ find ./ -name not-a-dir | xargs echo cd
cd ./not-a-dir
c@CW8:~/d/bin/try$ find ./ -name not-a-dir | xargs cd
xargs: cd: No such file or directory
CW8:~/d/bin/try$ find ./ -name not-a-dir | cd
c@c@CW8:~/d/bin/try$ cd not-a-dir
bash: cd: not-a-dir: Not a directory
c@CW8:~/d/bin/try$ /bin/ls -l dir 
/bin/ls: cannot access dir: No such file or directory
c@CW8:~/d/bin/try$ find ./ -name dir -type d
c@CW8:~/d/bin/try$ mkdir dir
c@CW8:~/d/bin/try$ find ./ -name dir -type d
./dir
c@CW8:~/d/bin/try$ find ./ -name dir -type d | xargs echo cd
cd ./dir
c@CW8:~/d/bin/try$ find ./ -name dir -type d | xargs cd
xargs: cd: No such file or directory
find ./ -name dir -type d | xargs cd
c@CW8:~/d/bin/try$
In the above, note that find ./ -name not-a-dir | cd produces no output, silently returning to the prompt while the following cd not-a-dir command results in an error from bash.

Hey -- a useful new debugging technique to expose errors on commands run by "find"!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File name expansion with {} in gnu find and xargs anamericanjoe Linux - Software 2 09-16-2006 04:31 PM
clarification on find + xargs rm command dtra Linux - General 2 05-17-2006 06:56 AM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 09:21 AM
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 02:42 AM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 12:22 PM


All times are GMT -5. The time now is 06:54 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration