SSH unzip AND change the filename (or get the filename contained therein) - ssh

/usr/bin/curl http://somewebsite.com/foo.zip -o 4232.zip
unzip -o -q -L 4232.zip
chown 508 /home/me/www/inbound/data/??????.xml
rm -f 4232.zip
I am using this SSH script to download a zip file called foo.zip, rename the file to 4232.zip, the extract the contents.
My problem is that the zip file contains a single file whose name is constantly changing. I cannot see a flag for unzip that lets me rename the file(s) inside the zip.
How can I rename the mystery file inside. There is really only ever one file in my immediate project.
-or-
How can I get that filename so I can change ownership and use it in a PHP script that will process it later on...
Any help would be appreciated.

This will tell you the name of the .xml file in the zip
unzip -l z.zip | grep -o '[^/]*\.xml'
If you want to extract and rename the xml file
unzip -p z.zip \*.xml | cat > NEWFILE.xml

Related

Extract huge tar.gz archives from S3 without copying archives to a local system

I'm looking for a way to extract huge dataset (18 TB+ found here https://github.com/cvdfoundation/open-images-dataset#download-images-with-bounding-boxes-annotations) with this in mind I need the process to be fast (i.e. I don't want to spend twice the time for first copying and then extracting files) Also I don't want archives to take extra space not even one 20 gb+ archive.
Any thoughts on how one can achieve that?
If you can arrange to pipe the data straight into tar, it can uncompress and extract it without needing a temporary file.
Here is a example. First create a tar file to play with
$ echo abc >one
$ echo def >two
$ tar cvf test.tar
$ tar cvf test.tar one two
one
two
$ gzip test.tar
Remove the test files
$ rm one two
$ ls one two
ls: cannot access one: No such file or directory
ls: cannot access two: No such file or directory
Now extract the contents by piping the compressed tar file into the tar command.
$ cat test.tar.gz | tar xzvf -
one
two
$ ls one two
one two
The only part missing now is how to download the data and pipe it into tar. Assuming you can access the URL with wget, you can get it to send the data to stdout. So you end up with this
wget -qO- https://youtdata | tar xzvf -

Cannot read .7z file in Google Colab

I am using Google Colab to create a deep learning model, and I face an issue when I run this code at the first time.
!p7zip -d filename.7z
I get the following message:
/usr/bin/p7zip: cannot read filename.7z
But when I re-run the same cell again, the code works.
Do you know what is the reason of this issue?
First, you have to specify path before the file name
in my case
!mkdir ~/data
!cd ~/data
!mkdir planet
!cd planet
# -c: competition name
# -f: which file you want to download
# -p: path to where the file should be saved
!kaggle competitions download -c planet-understanding-the-amazon-from-space -f train-jpg.tar.7z -p ~/data/planet/
# Unzip the 7zip files
# -d: which file to un7zip
!p7zip -d ~/data/planet/test-jpg.tar.7z

Mac Terminal /bin/settitle.sh: No such file or directory

I'm trying to create a bash script to change the titles of my terminal windows so I can identify what they are doing. I spent a few hours on this and cant figure it out. The idea is to be able to execute settitle NewTitle. Thank you.
This is my echo:$PATH. It looks like Users/klik/bin is there twice. Maybe that is the issue?
~ klik echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/klik/bin:/Users/klik/bin
This is the script which was created in textedit in plain text format.
#!/bin/bash
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo -e "\033]0;${1}\007\c"
This is my bash_profile and bin file.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH=$PATH:$HOME/bin
alias desk='cd ~/Desktop/'
alias down='cd ~/Downloads/'
alias github='cd ~/github/'
This is my ls -a output
Current directories
~ klik ls -l $HOME/bin | pbcopy
total 8
-rwx--x--x# 1 klik staff 147 Mar 9 21:39 settitle.sh
Try this:
echo -e "\033]0;FreddyFrog\007\c"
You need to use -e to turn on interpretation of escape characters. You can also use printf.
printf '\033]0;%s\007\015' "Hippo Croco Horror Pig"
This issue above was that the file was saved with .txt extension. I dont know why this was the case given the ls command showed a .sh ext. At any rate, this is the process I used for creating this script and and executing it.
Open Finder -> Applications->TextEdit in Mac.
Select New Document at bottom left.
From menu select Format -> Make Plain Text
Paste in this code:
#!/bin/sh
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo "\033]0;${1}\007\c"
Thanks to Alvin Alexander for the code.
Still in TextEdit select menu File -> Save
Uncheck "If no extension is provided, use ".txt" "
When I chose my file name I saved it with no extension so i could just type the command settitle NewTitle without having to type the extension every time.
Note the folder the file is being saved to. It defaults to desktop on my machine.
Open Finder -> Go -> Go to Folder
Type in the path to your User Bin folder: mine was /Users/klik/bin
You can check to see if you have a User/bin folder by running: ls -l from your home directory.
If you don't have a bin folder in this directory you can create one by going to your $HOME directory and executing:
mkdir bin
To find out what is your home directory see this
You can then open the directory by executing:
open bin
This will open the folder in Finder.
Drag the script file you created into this folder.
Make sure the script is executable by executing the following command from the folder the file is in or by including the path to the file in name of file:
chmod +x <name of file>
Make sure that the script is in your executable $PATH by executing:
echo $PATH
You will get something like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/<you>/bin
If you dont see the path of your script, ie /Users/''/bin, then the script file is not in your executable path and you need to put it in your .bash_profile. Execute ls -l to see if you have a .bash_profile file.
ls -l
If you don't have one, make sure your are in your $HOME directory then create one by executing:
mkdir .bash_profile
Open your .bash_profile file in your default editor:
open .bash_profile
Or open with nano (to save and close nano see this link):
nano .bash_profile
Add the following line to the .bash_profile then save/close:
export PATH=$PATH:$HOME/bin
Exit the terminal to reset by executing:
exit
Open the terminal then type:
settitle <whateveryouwant>
I hope this saves someone some time. Thanks to Mark Setchell for his constructive help.

Downloading .j2k or .png files using wget:if else condition

I am downloading folder consisting of either j2k or png file using wget.
Now i want while downloading folder if user is requesting .j2k file and if .j2k file is not existing in that folder then by default download .png file.
i.e. i want download j2k if present || download .png file.
I have used like this
wget -d any.com -i /folder -r -l 1 -nc -A j2k,png
-d: download from this Domain
-i: download from this foldern
-r: recursive
-l 1: follow only 1 link deep
-nc: no clobber = download only if file doesn't exist
-A: accept/download only all *.ogg and *.mp3
but using this it is downloading both j2k and png.
Any help will be appreciated.
Referred Links:
wget if else download condition
wget manual

Bazaar: How to export just changed file of some specific revision?

I'm wonder if there is anyway to just export files that have changed in specific revision.
e.g : I have branch with three files :
file.php
file.js
file.css
Just file.js has changed in last commit.
How to use export command to just export changed file (file.js) and prevent exporting others.
Is there any Plugin or external 3rdParty ?
Using bzr export you can specify a single directory to export, but not individual files.
As an alternative, you can get the contents of a file at some past revision like this:
bzr cat -r REV path/to/file > file.rREV
You can get the list of changed files at some past revision with the one-liner:
bzr diff -c REV | grep ^===
To wrap it up, here's a complete one-liner that does just what you asked for: export just the modified files of some specific revision REV into a directory called EX:
bzr diff -cREV | grep '^=== modified file ' | sed -e "s/[^']*//" -e "s/'//g" |\
while read fname; do echo $fname; mkdir -p EX/"$(dirname "$fname")";\
bzr cat -rREV "$fname" > EX/"$fname"; done
It loops over the modified files in revision REV, prepares the export directory EX with all parent directories needed to save the file preserving the path, and finally gets the file with bzr cat and writes it at the correct relative path inside EX.