In Ubuntu, is it possible to launch a "select file" dialog from the command line? I need to find a command that launches the "select file" dialog (so that I can prompt the user to navigate to a file using a shell script, and then return the file path.)
Use zenity
More reference here: http://library.gnome.org/users/zenity/stable/file-selection.html.en
I use kdialog:
kdialog --getopenfilename . 'audio/ogg audio/mp3 audio/wav'
kdialog --getsavefilename . 'audio/ogg audio/mp3 audio/wav'
To open multiple files:
variable=$(kdialog --getopenfilename --multiple . 'audio/ogg audio/mp3 audio/wav');
variable=$(echo $variable | sed 's/\ \//\\n\//g')
Related
My aim is to create a shell script such that it logins and filter the list of files available and select a file to get. Here I need to run commands like in bash.
My sample code is:
sshpass -p password sftp user#10.10.10.10 <<EOF
cd /home/
var=$(ls -rt)
echo $var
echo "select a folder"
read folder
cd $folder
filen=&(ls -rt)
echo $filen
echo "select a file"
read name
get $name
bye
EOF
The above approach will not work. Remember that the 'here document' (<<EOF ... EOF) is evaluate as input to the sftp session. Prompts will be displayed, and user input will be requested BEFORE any output (ls in this case) will be available from sftp.
Consider using lftp, which has more flexible construct. In particular, it will let you use variables, create command dynamically, etc.
lftp sftp://user#host <<EOF
cd /home
ls
echo "Select Folder"
shell 'read folder ; echo "cd $folder" >> temp-cmd'
source temp-cmd
ls
echo "Select Folder"
shell 'read file ; echo "get $file" >> temp-cmd'
source temp-cmd
EOF
In theory, you can create similar constructs with pipes and sftp (may be a co-process ?), but this is much harder.
Of course, the other alternative is to create different sftp sessions for listing, but this will be expensive/inefficient.
After some research and experimentation, found a way to create batch/interactive sessions with sftp. Posting as separate answer, as I still believe the easier way to go is with lftp (see other answer). Might be used on system without lftp
The initial exec create FD#3 - pointing to the original stdout - probably user terminal. Anything send to stdout will be executed by the sftp in the pipeline.
The pipe is required to allow both process to run concurrently. Using here doc will result in sequential execution. The sleep statement are required to allow SFTP to complete data retrieval from remote host.
exec 3>&1
(
echo "cd /home/"
echo "ls"
sleep 3 # Allow time for sftp
echo "select a folder" >&3
read folder
echo "cd $folder"
echo "ls"
sleep 3 # Allow time for sftp
echo "select a file" >&3
read name
echo "get $name"
echo "bye"
) | sshpass -p password sftp user#10.10.10.10
I would suggest you to create a file with pattern of the files you want downloaded and then you can get files downloaded in one single line:
sftp_connection_string <<< $"ls -lrt"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e 's/^/get -P /g'|sftp_connection_string
if there are multiple definite folders to be looked into, then:
**Script version**
for fldr in folder1 folder2 folder3;do
sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}/g"|sftp_connection_string
done
One-liner
for fldr in folder1 folder2 folder3;do sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}\//g"|sftp_connection_string;done
let me know if it works.
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)
Creating a service in Automator (Run shell script > Bash) to copy to clipboard filesize (in bytes) of selected file in Finder. I'm not very well versed in bash, and can't see where this is wrong.
Aim is to have it appear in the 'services' menu when right clicking a file, and then I can just paste the filesize wherever I fancy.
Options I have selected in Automator are:
Service receives selected "files or folders" in "Finder.app"
Shell /bin/bash
Pass input as arguments
for f in "$#"
do
getty=$(ls -l "$f")
done
IFS=' ' read -a newList <<< "${getty}"
echo -n ${newList[4]} | pbcopy
Run Shell Script failed - 1 error:
-: -c: line1: syntax error near unexpected token `do
Instead of using ls, use stat to get the file size.
stat -f '%z' "$f" | pbcopy
To copy, say, the name and size of a group of files to the clipboard:
stat -f '%N %z' "$#" | pbcopy
To make it clearer to everyone, the solution is :
create an empty automator script
add action "get selected elements from Finder
add action "Run shell script" with inputs as arguments
with the following script :
stat -f '%z' "$#" | pbcopy
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}"
I am writing a shell script that prompts the user for a file path:
read -e -p "Enter the path to the file: " FILEPATH
I am then using this file path to perform operations – namely to compress a folder.
(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;
At the prompt, if I use the ~ alias (home directory), then the shell script doesn't seem to understand this, as the tar function compresses the wrong path. Is there anyway I can allow for this alias?
Also, tab completion seems to be case-sensitive at the prompt. I was wondering how I can change that?
Example using eval:
read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
cd $FILEPATH
echo $PWD
In your case it becomes:
read -e -p "Enter the path to the file: " FILEPATH
eval FILEPATH=$FILEPATH
(cd "$FILEPATH"; tar -cvz *) > /tmp/torrent.tar.gz;
To deal with spaces you can use sed:
read -e -p "Enter the path to the file: " FILEPATH
FILEPATH=$(echo $FILEPATH | sed 's/ /\\ /')
eval FILEPATH=$FILEPATH
cd "$FILEPATH"
echo $PWD
You could apply the substitution yourself like this:
filepath=${filepath/\~/$HOME}
I don't know whether there's a way to get the shell to do it for you.
Here's an answer to your other question: https://superuser.com/questions/90196/case-insensitive-tab-completion-in-bash