You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

-bash: cat: command not found (or grep)

Hi, Strange things:




i3: okn$ ls -R | grep -h "dof" | cat > dtext.txt

-bash: cat: command not found


or then I get this:


i3: okn$ find . -type f -print | grep -c

-bash: grep: command not found


but this is not happening all the time, i remember grep or cat did work once or twice...



What's going on here?


OS 10.7.4; repaired permissions


Any assistance much appreciated!


/ bw, Omar KN

Mac OS X (10.7.2)

Posted on Jul 9, 2012 2:52 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 9, 2012 3:04 PM

You have corrupted your PATH environment variable which tells the bash where to look for commands.


Start looking in one of the following files, as that is most likely where you were messing with PATH


.bash_profile

.bash_login

.profile

.bashrc


Fix your PATH and then restart your Terminal session.


If necessary, use commands such as


/bin/ls -a

/bin/cat .filename

/usr/bin/nano .filename


Message was edited by: BobHarris

19 replies
Question marked as Top-ranking reply

Jul 9, 2012 3:04 PM in response to oktan

You have corrupted your PATH environment variable which tells the bash where to look for commands.


Start looking in one of the following files, as that is most likely where you were messing with PATH


.bash_profile

.bash_login

.profile

.bashrc


Fix your PATH and then restart your Terminal session.


If necessary, use commands such as


/bin/ls -a

/bin/cat .filename

/usr/bin/nano .filename


Message was edited by: BobHarris

Jul 10, 2012 12:18 AM in response to BobHarris

Alright Bob,


There was only the .bash_history file on Lion, so I created a .bash_profile in my home folder and put in some aliases.


But which Path to set escapes me, I did a look-up on Google but don't know what exactly to write to get grep and cat working correctly.


Any assistance much appreciated!


/ bw, Omar KN

Jul 10, 2012 5:23 AM in response to oktan

Having an incorrect PATH established is a very common problem, and is fodder for a number of threads here as folks lose access to the various tools.


In general, PATH is a colon-seperated list of directories where bash will look for commands.


When PATH is incorrect, the easiest approach is to remove or review the modifications; to start over.


Whenever PATH is corrupted, you'll have to specify the full file paths to the tools you need. Explicitly. Or you'll need to use another session under a different user, or you can use a GUI-based shell-compatible text editor such as MacVim or TextWrangler. (You cannot use a GUI-based editor that write files that use any character encodings or file formats that the shell can't read.)


Here is an example of specifying an explicit path for the tools, and also how to locate where the tools are located, and how to use the pico, err, nano text editor to edit your .bash_profile file:


$ /usr/bin/whereis grep

/usr/bin/grep

$ /usr/bin/nano ~/.bash_profile


Once you edit your .bash_profile and remove or # comment out the PATH changes, exit the terminal session and restart.


Here is a bash introduction and here is advanced bash scripting. (Those are the HTML versions, There are PDFs of those documents over there, too.) There is the Apple introduction to the command line (10.6) manual, and the 10.7 command line is little different from that of 10.6.

Jul 10, 2012 7:01 AM in response to MrHoffman

ALright,


I found the Path here:


i3:m okn$ echo $PATH

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin


so I set in my home pico:


i3:~ okn$ pico ~/.bash_profile


export PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin


then I quit the Terminal and reopened, still cannot find grep!


But with your help I'm almmost there (?).


bw, OKN

Jul 10, 2012 7:09 AM in response to Mark Jalbert

Hi & good day,


1.)

echo $PATH


gives me:


/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/bin:/bin:/usr/sbi n:/sbin:/usr/local/bin:/usr/X11/bin


(this became even longer now and redundant so it seems after I set this PATH in .bash_profile)


______________________________________________________________________________


2.)


i3:~ okn$ /bin/launchctl getenv PATH

/usr/bin:/bin:/usr/sbin:/sbin


(this is what I entered in .bash_profile, see above)


______________________________________________________________________________


3.)


i3:~ okn$ /bin/cat /private/etc/paths

/usr/bin

/bin

/usr/sbin

/sbin

/usr/local/bin


(so this means that cat is in all these 5 folders?! ... )


______________________________________________________________________________

and:

4.)


i3:~ okn$ /bin/ls /private/etc/paths.d/

50-X11


______________________________________________________________________________


You may see something there hopefully!


bw, OKN

Jul 10, 2012 7:17 AM in response to oktan

What happens when you issue the command


/usr/bin/which grep

/usr/bin/which cat


/bin/ls -aleO@ /usr/bin/grep

/bin/ls -aleO@ /bin/cat


/usr/bin/file /usr/bin/grep

/usr/bin/file /bin/cat


Your PATH looks OK, so if you cannot find grep and cat, then it would imply the commands have either been removed from your system, or are no longer executable images

Jul 10, 2012 12:41 PM in response to BobHarris

>What happens when you issue the command


Here are my results so far:



i3:~ okn$ /usr/bin/which grep

/usr/bin/grep



i3:~ okn$ /usr/bin/which cat

/bin/cat



i3:~ okn$ /bin/ls -aleO@ /usr/bin/grep

-rwxr-xr-x 1 root wheel compressed 232064 Jan 15 17:51 /usr/bin/grep



i3:~ okn$ /bin/ls -aleO@ /usr/bin/cat

ls: /usr/bin/cat: No such file or directory



i3:~ okn$ /usr/bin/file /usr/bin/grep

/usr/bin/grep: Mach-O universal binary with 2 architectures

/usr/bin/grep (for architecture x86_64): Mach-O 64-bit executable x86_64

/usr/bin/grep (for architecture i386): Mach-O executable i386



i3:~ okn$ /usr/bin/file /usr/bin/cat

/usr/bin/cat: cannot open `/usr/bin/cat' (No such file or directory)


also this:

i3:~ okn$ /usr/bin/file /bin/cat

/bin/cat: Mach-O universal binary with 2 architectures

/bin/cat (for architecture x86_64): Mach-O 64-bit executable x86_64

/bin/cat (for architecture i386): Mach-O executable i386


Does this show something is wrong??


Thank you guys so far!


(On my way .... to finally use grep and cat again)

bw,

Omar KN

Jul 10, 2012 1:01 PM in response to oktan

'cat' is stored in /bin/cat, NOT /usr/bin/cat, which explains the file not found messages above.

HOWEVER, the information you have provided seems to imply you have the executables, and previous information provides says you have a valid PATH:


echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/bin:/bin:/usr/sbi n:/sbin:/usr/local/bin:/usr/X11/bin


this creates a mystery as to why you are getting:


-bash:  cat: command not found
-bash:  grep: command not found


I have a couple of thoughts.


a) I'm wondering if you have any 'alias' statements in your .bash_profile (or .bash_login, or .profile, or .bashrc).


b) if you would get the same results should you rename your .bash_profile (and/or .bash_login, .profile, .bashrc) to something like saved.bash_profile, then restart your terminal session (basically eliminating any personalized shell customization).


c) what would happen if you created a new account (System Preferences -> Accounts) - basically a clean starting point.


d) are you executing these commands from the command prompt? Or from within a shell script?

d.1) if from within a shell script, is the script file the correct file format.


file name.shell.script


you want to check that the output from the 'file' command DOES NOT say "...with CRLF line terminators...", as that would imply the the file came from (or through) a Windows system.

Jul 10, 2012 10:48 PM in response to Frank Caggiano

Thank you guys!


Frank C. referred to bash: command not found after piping

(when typing really slowly and waiting a few secs after typing the pipe it's working!!)


Solution:

After writing the pipe, if I count to three and then continue typing ( f ex cat > file.txt) it _does_ work!!!


ie. bash will again find the commands of grep and cat!


Thank God!


/bw,

Omar K N

(who on the way learned something more about the elegant UNIX-command line!)

Jul 11, 2012 6:26 AM in response to oktan

You should NOT need to type slowly to enter commands on the command line.


As I recall the problem that Frank Caggiano is referencing was caused by a bad keyboard.


You could try the following experiment:


cat -vte
anything | something | even more | with fake options -a -b -c | and even | more | junk <RETURN>
do it | again | and some| more <RETURN>
each | time you | hit return | cat -vte | will display the line | with any <RETURN>
invisible or control | characters | displayed so | you can | see them <RETURN>
<Control-D> # terminates the cat command.


The cat -vte will display invisible and control characters that you might not normally see as you type. If you keyboard is injecting extra junk when you type something, it will be visible after you enter <RETURN> on each line.


If you have another keyboard, plug it is and give that a try.


Message was edited by: BobHarris

-bash: cat: command not found (or grep)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.