Store a filename in a variable minus the extension
File: test.zip
Bash script
while read filename; do
zip_file=${filename}
# do stuff
done;
Value stored in variable = "test"
bash scripts
add a comment |
File: test.zip
Bash script
while read filename; do
zip_file=${filename}
# do stuff
done;
Value stored in variable = "test"
bash scripts
add a comment |
File: test.zip
Bash script
while read filename; do
zip_file=${filename}
# do stuff
done;
Value stored in variable = "test"
bash scripts
File: test.zip
Bash script
while read filename; do
zip_file=${filename}
# do stuff
done;
Value stored in variable = "test"
bash scripts
bash scripts
asked May 24 '15 at 14:00
JohnnyBizzleJohnnyBizzle
1702418
1702418
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Use bash parameter expansion:
zip_file="${filename}"
new_name="${zip_file%.*}"
new_namewill contain the nametestif thezip_filehastest.zip
If the
zip_filehastest.foo.zip,new_namewill havetest.foo, if you want onlytestout oftest.foo.zipuse:
new_name="${zip_file%%.*}"
This seems to include part of the path. I just want the file name. eg NOT./Somefolder/Somefolder/FilenameminusExtnbutFilenameminusExtn
– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename astest.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..
– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need$(basename "${zipfile}" .zip)to get what you want.
– muru
May 24 '15 at 16:42
add a comment |
heemayl is still right: example:
full_path=/foo/bar/baz.zip
file_name="${full_path##*/}"
name="${file_name%.*}"
add a comment |
Using sed:
zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"
zip_file="$( [...] )": assigns thestdoutof an invoked subshell to the variablezip_fileas a string
<<< "${filename}" [...]: redirects the content of the variable${filename}to the invoked subshell'sstdinas a string
sed -r 's/^(.*)./1/': edits the content of the invoked subshell'sstdinusing extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot
Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:
zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f627678%2fstore-a-filename-in-a-variable-minus-the-extension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use bash parameter expansion:
zip_file="${filename}"
new_name="${zip_file%.*}"
new_namewill contain the nametestif thezip_filehastest.zip
If the
zip_filehastest.foo.zip,new_namewill havetest.foo, if you want onlytestout oftest.foo.zipuse:
new_name="${zip_file%%.*}"
This seems to include part of the path. I just want the file name. eg NOT./Somefolder/Somefolder/FilenameminusExtnbutFilenameminusExtn
– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename astest.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..
– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need$(basename "${zipfile}" .zip)to get what you want.
– muru
May 24 '15 at 16:42
add a comment |
Use bash parameter expansion:
zip_file="${filename}"
new_name="${zip_file%.*}"
new_namewill contain the nametestif thezip_filehastest.zip
If the
zip_filehastest.foo.zip,new_namewill havetest.foo, if you want onlytestout oftest.foo.zipuse:
new_name="${zip_file%%.*}"
This seems to include part of the path. I just want the file name. eg NOT./Somefolder/Somefolder/FilenameminusExtnbutFilenameminusExtn
– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename astest.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..
– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need$(basename "${zipfile}" .zip)to get what you want.
– muru
May 24 '15 at 16:42
add a comment |
Use bash parameter expansion:
zip_file="${filename}"
new_name="${zip_file%.*}"
new_namewill contain the nametestif thezip_filehastest.zip
If the
zip_filehastest.foo.zip,new_namewill havetest.foo, if you want onlytestout oftest.foo.zipuse:
new_name="${zip_file%%.*}"
Use bash parameter expansion:
zip_file="${filename}"
new_name="${zip_file%.*}"
new_namewill contain the nametestif thezip_filehastest.zip
If the
zip_filehastest.foo.zip,new_namewill havetest.foo, if you want onlytestout oftest.foo.zipuse:
new_name="${zip_file%%.*}"
edited Jan 26 at 18:00
answered May 24 '15 at 14:05
heemaylheemayl
67.2k9142214
67.2k9142214
This seems to include part of the path. I just want the file name. eg NOT./Somefolder/Somefolder/FilenameminusExtnbutFilenameminusExtn
– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename astest.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..
– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need$(basename "${zipfile}" .zip)to get what you want.
– muru
May 24 '15 at 16:42
add a comment |
This seems to include part of the path. I just want the file name. eg NOT./Somefolder/Somefolder/FilenameminusExtnbutFilenameminusExtn
– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename astest.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..
– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need$(basename "${zipfile}" .zip)to get what you want.
– muru
May 24 '15 at 16:42
This seems to include part of the path. I just want the file name. eg NOT
./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn– JohnnyBizzle
May 24 '15 at 14:39
This seems to include part of the path. I just want the file name. eg NOT
./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn– JohnnyBizzle
May 24 '15 at 14:39
@JohnnyBizzle With due respect why would you give the filename as
test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..– heemayl
May 24 '15 at 14:42
@JohnnyBizzle With due respect why would you give the filename as
test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..– heemayl
May 24 '15 at 14:42
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
I think you just need $(basename "${zip_file}") to get what I want.
– JohnnyBizzle
May 24 '15 at 15:34
@JohnnyBizzle no, you would need
$(basename "${zipfile}" .zip) to get what you want.– muru
May 24 '15 at 16:42
@JohnnyBizzle no, you would need
$(basename "${zipfile}" .zip) to get what you want.– muru
May 24 '15 at 16:42
add a comment |
heemayl is still right: example:
full_path=/foo/bar/baz.zip
file_name="${full_path##*/}"
name="${file_name%.*}"
add a comment |
heemayl is still right: example:
full_path=/foo/bar/baz.zip
file_name="${full_path##*/}"
name="${file_name%.*}"
add a comment |
heemayl is still right: example:
full_path=/foo/bar/baz.zip
file_name="${full_path##*/}"
name="${file_name%.*}"
heemayl is still right: example:
full_path=/foo/bar/baz.zip
file_name="${full_path##*/}"
name="${file_name%.*}"
answered May 24 '15 at 15:00
StephenStephen
1,657711
1,657711
add a comment |
add a comment |
Using sed:
zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"
zip_file="$( [...] )": assigns thestdoutof an invoked subshell to the variablezip_fileas a string
<<< "${filename}" [...]: redirects the content of the variable${filename}to the invoked subshell'sstdinas a string
sed -r 's/^(.*)./1/': edits the content of the invoked subshell'sstdinusing extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot
Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:
zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"
add a comment |
Using sed:
zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"
zip_file="$( [...] )": assigns thestdoutof an invoked subshell to the variablezip_fileas a string
<<< "${filename}" [...]: redirects the content of the variable${filename}to the invoked subshell'sstdinas a string
sed -r 's/^(.*)./1/': edits the content of the invoked subshell'sstdinusing extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot
Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:
zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"
add a comment |
Using sed:
zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"
zip_file="$( [...] )": assigns thestdoutof an invoked subshell to the variablezip_fileas a string
<<< "${filename}" [...]: redirects the content of the variable${filename}to the invoked subshell'sstdinas a string
sed -r 's/^(.*)./1/': edits the content of the invoked subshell'sstdinusing extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot
Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:
zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"
Using sed:
zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"
zip_file="$( [...] )": assigns thestdoutof an invoked subshell to the variablezip_fileas a string
<<< "${filename}" [...]: redirects the content of the variable${filename}to the invoked subshell'sstdinas a string
sed -r 's/^(.*)./1/': edits the content of the invoked subshell'sstdinusing extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot
Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:
zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"
edited May 24 '15 at 15:36
answered May 24 '15 at 15:25
koskos
25.8k871121
25.8k871121
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f627678%2fstore-a-filename-in-a-variable-minus-the-extension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown