For a couple of weeks, i create a script (bash) to find some files with the terminal's mac.
How to open the results in a folder interface of mac with all the files found ? (ie like the finder of mac)
I try the command xargs with the command open but each file found is open, i want to find all the result in a same screen because i can have a preview, etc.
this is my script :
grep -irEl --exclude-dir={"*sys"} "word to find" /Users/ponne/Desktop/Test | xargs ls -lah
does this solve your problem?
ls -alh $( grep -irEl --exclude-dir={"*sys"} "word to find" /Users/ponne/Desktop/Test)
Related
i have project in which i have to play a number of sound files at random, at the push of a button iam using an arduino yun. so far i can play the sounds from the sd card, however i want the random number to generate depending on the number of files in the sd card.i need help in reading the number of files through the command shell from the sketch
cd /mnt/sda1
ls | wc - l
when i this in the terminal i get the answer 4 and thats exactly the answer i need. i want to run this in the script, so far i have this however it doesnt seem to work
d.runShellCommand("ls | wc -l | cd /mnt/sda1");
result = d.parseInt();
Serial.println(result);
can anyone help me in fixing this
thank you
Your shell command has a few errors in it. Rather than pipe to a cd command, you can simply specify the directory when running ls. for example:
ls | wc -l | cd ~/Documents/
outputs nothing. However,
ls ~/Documents/ | wc -l
Outputs the number of files and folders in my documents folder.
So this command should work:
d.runShellCommand("ls /mnt/sda1| wc -l");
For example, for command-line programs I just use
which git // return path of git
Is there something similar for programs like sublime?
Obviously, I could poke around and find it but I was hoping there was something like which
Not entirely sure what you want but if you have locate activated you can do a
locate -i -l 1 "sublime text 2.app"
or
locate -i -l 1 "sublime text.app"
depending on version (2 or 3-beta).
Remove -l 1 på list all matches for the text.
If locate is not active you might want to do a
find / -name "Sublime Text 2.app" 2> /dev/null
or
find / -name "Sublime Text.app" 2> /dev/null
If I run this in the terminal (Mac OSX):
vim dump.md -c 'vsplit poa.md' -c 'split one.md' -c ':wincmd h'
It works great (creates three windows with those files).
If I have it as part of a bash script, there's a problem. After doing what it's supposed to do, it leaves me with this in the vim status line:
1:dump.md 2:poa.md 3:one.md
1:dump.md 2:poa.md 3:one.md
Press ENTER or type command to continue
I have to press ENTER 6 times to clear the status line and return to editing (each ENTER outputs the above again).
Anyway around this?
Also, not sure if this is significant, but vim is aliased to mvim (MacVim).
EDIT
Here is the bash script, although I don't think it matters I've run the whole script separately before manually putting the vim dump.md... command it, and it works fine.
13
12 #!/bin/bash
11
10 cd ~/Dropbox/Journal/dumps/
9
8 LAST=`ls -tAF | grep '/$' | head -1`
7 NEW=dump-$(date +"%Y-%m-%d_%T")
6
5 mkdir $NEW
4 cd $NEW
3
2 cp ../$LAST/poa.md ./poa.md
1
14 vim dump.md -c 'vsplit poa.md' -c 'split one.md' -c ':wincmd h'
1
At first:
ls -al
And see just your file is existed...If your file is test make sure vim doesn't cread such files:
.test OR test~ OR something like these
Then :
vim file
Do your changes...
After that close it by saying:
:wq!
Then try to open it again...The problem should be solved
UPDATE
try this one:
ls -a | grep swp
if there was a file like : .script.swp or something like these you should remove them...
Your problem would be solved...You should see if your script it existed with .script.... and you should remove it...
If you can't find files start with . try to move all files(scripts) to another directory then work with them..and use :q! to quite and use :wq! to save and quite...
EDIT: I found a solution for this that worked. It's apparently important to quote the file paths to make sure they are read as a whole.
path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"
I've written a command I want to use in osx's terminal. It gets a list of file and folder paths in a directory, shuffles them, and then gets the path that is in the first line of the txt after the shuffle.
This is what I've got so far: [1]
gfind ~/x/y/ | gshuf | ghead -n 1
With gfind ~/x/y/ | gshuf | ghead -n 1 > ~/Desktop/z.txt I get a file path in this format /Users/me/x/y/some folder/some file.txt Instead of writing the path of this file or folder to a .txt I want to open it as if I just double clicked on it in finder. How can I do that? I thought the open command[2] was the right one based on the description in the man page, but I'm not sure exactly how to use it. How should I use it? Or, if it's the wrong command, which command should I use?
[1]
I'm using gnu coreutils via macports which is why there's a g in front of the familiar command names
[2]
open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
To have the folder returned in the results opened in Finder, this should work:
open -a Finder `gfind ~/x/y/ | gshuf | ghead -n 1`
This works for me:
path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"
When securing a Drupal or WordPress installation on a shared host that does not expose SSH access (a lousy situation, fwiw) lftp seems like the right approach to batch setting permissions for directories and files. The find command boasts that you can redirect its output, so one should be able to run a find, grep exclude to only match lines ending in "/" meaning a directory, and then set the permissions on such matches to 755 and perform the inverse on file matches and set to 644 and then fine tune specific files, such as settings.php and so forth.
lftp prompt> find . | grep "/$" | xargs chmod -v 755
Isn't working and I'm sure I have failed to chain these commands in the correct sequence and format.
How to get this to work?
Update: by "isn't working" I mean that the above command produces no output to the console, nor to the lftp error log. It isn't running these commands locally, fwiw. I'll reduce the command as a demonstration:
find . | grep "/$"
Will take the output of "find" and return matches, here, directories, by nature of the string match:
./daily/
./ffmpeg-installer/
./hourly/
./includes/
./includes/database/
./includes/database/mysql/
./and_so_forth_on_down
Which is cool, since I wish to perform a chmod (and internal command for lftp, with support varying by ftp server) So I expand the command like this:
find . | grep "/$" | xargs echo
Which outputs — nothing. No error output, either. The pipe from grep to xargs isn't happening.
My goal is to form the equivalent of:
chmod 755 ./daily/
chmod 755 ./ffmpeg-installer/
In lftp, the chmod command is performing an ftp-server-permissions change, not a local perms change.
For an explanation of why this does not work as expected, read on - for a solution to the given problem, scroll down.
The answer can be found in the manpage for lftp, which states that
"[s]ome commands allow redirecting their output (cat, ls, ...) to file or via pipe to external command."
So, when you are using a pipe like this on a command that does support redirection in lftp, you are piping its output to your local tools, which will eventually result in chmod trying to change the permissions for a file/directory on our local machine, and most likely fail in case you don't coincidally have the same directory layout in your current directory locally - which is probably the problem you encountered.
The grep + xargs pipe does work, I just tested the following:
lftp> find -d 2 | grep "/$"
./
./applications/
./lost+found/
./netinfo/
./packages/
./security/
./systems/
lftp> find -d 2 | grep "/$" | xargs echo
./ ./applications/ ./lost+found/ ./netinfo/ ./packages/ ./security/ ./systems/
My wild guess is that it did not appear to work for you because you did not specify a max-depth to find and the network connection + buffering in the pipe got in the way. When I try the same on a directory containing many files/subfolders it takes really long to finish and print. Did the command actually finish for you without output?
But still, what you are trying to do is not possible. As I stated, the right-hand-side of the pipe works with external commands (even if an inbuilt of the same name exists) as explained by the manual, so
lftp> chmod 644 foobar
and
lftp> echo "foobar" | xargs chmod 644
are not equivalent.
Yes, chmod is an inbuilt but used in a pipe in the client it will not execute the inbuilt - the manpage clearly states this and you can easily test this yourself. Try the following commands and check their output:
lftp> echo foo | uname -a
lftp> echo foo | ls -al
lftp> echo foo | chmod --help
lftp> chmod --help
Solution
As far as a solution to your problem is concerned, you can try something along the lines of:
#!/bin/bash
server="ftp.foo.bar"
root_folder="/my/path"
{
{
lftp "${server}" <<EOF
cd "${root_folder}"
find | grep "/$"
quit
EOF
} | awk '{ printf "chmod 755 \"%s\"\n", $0 }'
{
lftp "${server}" <<EOF
cd "${root_folder}"
find | grep -v "/$"
quit
EOF
} | awk '{ printf "chmod 644 \"%s\"\n", $0 }'
} | lftp "${server}"
This logs in to your server, cds to the folder where you want to recursively start changing the permissions, uses find + grep to find all directories, logs out, pipes this file list into awk to build chmod commands around it, repeats the whole process for files and then pipes the whole list of commands into a new lftp invocation to actually run the generated chmod commands.
You will also have to add your credentials to the lftp invocations and you might want to comment out the final | lftp "${server}" to check if it produces the desired output before you actually run the whole thing. Please report back if this works for you!