Grabbing a files path and name without extension

Forum rules
Before you post please read how to get help
Post Reply
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Grabbing a files path and name without extension

Post by darkrchaos »

I been working on a bash file that extracts a zip file into a folder named the same as the zip.
For example. I would want video23.zip to extract its content into a folder next to it called video23.

This is what I got so far.
This extracts the zip into a folder next to the zip called NewFolder

Code: Select all

unzip -O shift-jis $1 -d $(dirname "$1")/NewFolder
After searching online I found this to be the code to get the filename with extension. But I'm not sure how to remove the extension and add this to my already line of code.

Code: Select all

$(basename "$1") 
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

If the zip extension is always the same you can use bash ${parameter%word} expansion. Please review the following example:

Code: Select all

$ echo $File
some_path/file.gz

$ echo ${File%.gz}
some_path/file

$ echo "$(basename ${File%.gz})"
file

$
I chose the .gz extension for my example.
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Re: Grabbing a files path and name without extension

Post by darkrchaos »

I saw that code while searching, but what if it's rar. I believe unzip still extracts that.
User avatar
Kadaitcha Man
Level 11
Level 11
Posts: 3936
Joined: Mon Aug 27, 2012 10:17 pm

Re: Grabbing a files path and name without extension

Post by Kadaitcha Man »

darkrchaos wrote:
Mon Mar 23, 2020 8:56 pm
I saw that code while searching, but what if it's rar. I believe unzip still extracts that.
If you needed to unpack rar, you could have said so the first time around.

man unrar
All kadaitcha men are liars (It's pronounced kad-eye-cha, not kada-itcha)
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Re: Grabbing a files path and name without extension

Post by darkrchaos »

Kadaitcha Man wrote:
Mon Mar 23, 2020 9:03 pm
darkrchaos wrote:
Mon Mar 23, 2020 8:56 pm
I saw that code while searching, but what if it's rar. I believe unzip still extracts that.
If you needed to unpack rar, you could have said so the first time around.

man unrar
No it's not that. I'm just saying I need code that removes more then a zip extension.
User avatar
MrEen
Level 23
Level 23
Posts: 17578
Joined: Mon Jun 12, 2017 8:39 pm

Re: Grabbing a files path and name without extension

Post by MrEen »

This page will point you in the right direction: https://stackoverflow.com/questions/121 ... t/12152997

Start reading below the accepted answer! And notice the change when you want to rename something with double extensions such as .tar.gz a few posts later.
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

If you give the full specification of what you are attempting, it would help with getting more a specific response. I do not like giving a new user instructions using wild cards. It can lead to chaos, because they have no concept of "what if" and much less how to test code. If you truly need to strip off any .extension, you can use a wild card. But you need to understand it and test it thoroughly. I cannot read your mind and assume what you want or actually need. When matching a pattern I try to be specific as possible to prevent any "uh oh" situations later on. Call me cautious. On other hand you can be more specific in your scope of the extension while using a wild card. For example, if you know your extension will always begin with a period and a lower case a through z letter, you can use that pattern along with a wild card to be more specific. And, I would encourage you to be as specific as possible. Please see the following dot extension wild card example:

Code: Select all

$ echo $File
some_path.12.zip

$ echo "$(basename ${File%.*})"
some_path.12

$ echo $FileB
some_path/file.20120323

$ echo "$(basename ${FileB%.*})"	
file

$

Using a more specific scope along with a wildcard to remove any dot extension beginning with a lower case a through z:

Code: Select all

$ echo $File
some_path.12.zip

$ echo "$(basename ${File%.[a-z]*})"
some_path.12

$ echo $FileB
some_path/file.20120323

$ echo "$(basename ${FileB%.[a-z]*})"
file.20120323

$
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Re: Grabbing a files path and name without extension

Post by darkrchaos »

I mainly wanted to make this because I zip and unzip a lot of japanese files and it got annoying running to the terminal every time. My plan was to make a bash file that would do everything for me. All I would have to do is right-click on the zip and click "Open With".

Code: Select all

unzip -O shift-jis $1 -d $(dirname "$1")/"$(basename ${FileB%.[a-z]*})"
So would this be right?

Also I would like to know the right way to test code. I'm sorry I just started using linux a month ago and I'm crazy busy learning csharp :oops:
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

edit 20200324@14:33
I was only giving you example of how to remove the dot extension. It was not meant to be your code. :roll: Again, I would encourage you to use very specific .zip extension pattern.
Assuming $1 is the file.zip to remove the extension you need to test this code:

Code: Select all

unzip -O shift-jis $1 -d "${1%.zip}"
My Linux Mint 19.1 XFCE unzip command does not mention the "-O shift-jis" option. Maybe that is something unique to your language pack?

Does not your file browser have a builtin archive manager? Most file browser if you right click a zip file it will have an extract here or extract to option. With most zip image files the extract option will create a directory with the same name as the zip file without the .zip extension. And, the extract to option will allow you to specify a new directory name. Is there a valid reason not to use this?

Which Linux Mint version and desktop environment are you running?
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Re: Grabbing a files path and name without extension

Post by darkrchaos »

Nope that code didn't work all. Is there a way I can test this code in the terminal so I can see what error it's getting? I feel like that would be a big help.

Shift-jis is a language pack that lets me open files with Japanese encoding. If I use the file browser version then the letters will be all messed up so I need to use the terminal to select the right language pack.

I'm using Linux Mint 19.2 Tina, environment cinnamon if I'm correct.
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

darkrchaos wrote:
Mon Mar 23, 2020 5:59 pm
...
I been working on a bash file that extracts a zip file into a folder named the same as the zip.
For example. I would want video23.zip to extract its content into a folder next to it called video23.
...
Ok, how did you test the bash file? Where is the bash file? What is the name of the test.zip file you are attempting to extract, as well as the location of the test.zip file? Please post the contents of the bash file using the </> button directives to make it easier to read. Once the proper information about the bash file (script) and the test file.zip has been established, you can move forward testing and debugging it.
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

darkrchaos wrote:
Mon Mar 23, 2020 5:59 pm
...
I been working on a bash file that extracts a zip file into a folder named the same as the zip.
For example. I would want video23.zip to extract its content into a folder next to it called video23.
...
Ok, how did you test the bash file? Where is the bash file? What is the name of the test.zip file you are attempting to extract, as well as the location of the test.zip file? Please post the contents of the bash file using the </> button directives to make it easier to read. Once the proper information about the bash file (script) and the test file.zip has been established, you can move forward testing and debugging it.
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
User avatar
darkrchaos
Level 1
Level 1
Posts: 26
Joined: Thu Mar 19, 2020 8:29 pm

Re: Grabbing a files path and name without extension

Post by darkrchaos »

deck_luck wrote:
Thu Mar 26, 2020 4:42 pm
darkrchaos wrote:
Mon Mar 23, 2020 5:59 pm
...
I been working on a bash file that extracts a zip file into a folder named the same as the zip.
For example. I would want video23.zip to extract its content into a folder next to it called video23.
...
Ok, how did you test the bash file? Where is the bash file? What is the name of the test.zip file you are attempting to extract, as well as the location of the test.zip file? Please post the contents of the bash file using the </> button directives to make it easier to read. Once the proper information about the bash file (script) and the test file.zip has been established, you can move forward testing and debugging it.
- Right clicking on the zip and clicking open with
- Desktop
- Not sure actually I already deleted it. I think it was something in Japanese letters.
- Bash file:

Code: Select all

#!/bin/bash
unzip -O shift-jis $1 -d "${1%.zip}"
User avatar
deck_luck
Level 6
Level 6
Posts: 1065
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Grabbing a files path and name without extension

Post by deck_luck »

Make a Test directory.
Place a small test.zip file and your myunzip bash script in the Test directory.
Modify the first line of your myunzip script by adding -x like the following:

Code: Select all

#!/bin/bash -x
From a terminal session change directory in to your Test directory. Run the following commands and post the results using the </> directive.

Code: Select all

pwd
ls -l 
unzip -t test.zip
./myunzip test.zip 
🐧Linux Mint 19 XFCE 💡Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️ Proverbs 4:7 Wisdom is the principal thing; therefore get wisdom: and with all thy getting get understanding.
Post Reply

Return to “Scripts & Bash”