How to get the last added folder to a directory [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I work in a software development company and every day upon my machine boot I have to execute the same commands to start coding. I recently decided to create a bash script to do that for me. The problem is that the commands I need to type have a single difference from one another, and that is the folder I need to access.
I always have to access a directory where it will contain folders with different versions of the company code (let's call it "codes" for the sake of the discussion), and everyday another folder is added to the "codes" directory (they update the company code everyday) with name as timestamp e.g. 2021-07-05-17-52-51.
To be able to create my automation script I need to be able to get into the "codes" directory and get the most recent folder added to it, or get the latest timestamp.
I am new to bash and I couldn't find answers on how to get the last added folder to a directory using bash or someway to use tab and get the last one.

You can use something like this:
directory=$(ls -At1 | head -n 1)
An explanation in parts:
ls -At1 lists sorted by time with one entry per line
head -n 1 returns the first entry
$(...) runs the command as a subshell, evaluates, and sets directory to the name of the item with the most recent modified datestamp. If you want to ignore hidden files and folders, you can lose the -A flag from ls.

Related

Perforce sync all files with a specific extension to revision #0 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I would like to be able to sync to revision #0 for all files with a specific extension (so they get deleted). These files are taking up too much space and I don't use them.
I have tried a few different things with no luck:
p4 sync //root/.../*.psd#0
p4 sync //root/...#0/*.psd
p4 sync //root/.../*.psd#0
The syntax you want is:
p4 sync //....psd#none
(#none is the idiomatic way to specify "no revision", but #0 and #0 should also work.)
The revision specifier always goes immediately after the file path, never in the middle of it. Providing a path like //root/...#0/*.psd should have gotten you an error like Invalid revision number '0/*.psd'.
Note that if your server is case-sensitive (the default if it's hosted on a Unix platform), all parts of a file path are case-sensitive, so you may need to do both .psd and .PSD to cover all your bases.
The following variations might work, with caveats:
p4 sync //.../*.psd#0 -- this works, but is slower due to the double wildcard. You almost never want to do .../* in place of simply ....
p4 sync //root/.../*.psd#0 -- this should also work, but only in a depot that is literally called root. The "root" of the repository (i.e. the parent "directory" of all depots) is just //. If you ran a command against a //root/... path and there is no root depot, you should have gotten an error like //root/... - must refer to client 'yourclient', which is the error you get if you try to reference a specific domain (//something) that isn't a depot and isn't your current client.

How to run 1 .sh from DVD drive, and 2nd .sh from hard disk? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Environment: Linux RedHat 5 with bash terminal
I need to automate a process to do 2 things:
Have user install a dvd (with scripts) that prompt user for input,
and create some directories on the hard drive.
Have a 2nd script scp some files to the hard drive directories, and
then automatically burns a data dvd.
Right now I need the user to manually cd to the step 2 and run a .sh2.
General procedure:
User inputs provided disc, mounts the dvd drive,and is prompted for
a "name". Reading the variable "name", a directory and sub
directories are created under /tmp/"name".
User must manually navigate to the /tmp/name dir and ./2.sh, to kick
off the 2nd script.
I am having two issues with fully automating this using export name and kicking off the 2nd .sh. The issue is the 2nd .sh requires the mounted dvd drive (that .sh1 is running from) to be ejected, and running .sh2 as & in the background completely skips over the scp data pull I am doing on .sh2.
So any ideas on how to export the .sh1 variable, eject the dvd with scripts, and then run the .sh2 script which can source the .sh1 (no longer running) script variable name?
I have considered writing a file with the variable name to the hard drive directory to solve the export variable issue (export name wasn't working), but I still haven't thought of how to make the .2sh automatically run from the /tmp/name dir (.sh2 & wasn't allowing scps/dvd burn to finish), perform the scp's, and allow my "burn dvd" .sh run.
You don’t provide enough details to work out all details of the answer. However:
1) If you want to unmount the DVD, neither the process invoking the first script, nor the first script itself (nor any other process) can have it as their current directory. This means, you cannot invoke the first script as cd /media/dvd; bash ./1.sh, but you have to do something like bash /media/dvd/1.sh
2) Just after creating /tmp/name, cd to it in the first script, create the second script, make it executable (don’t forget an appropriate shebang), and invoke it as exec ./2.sh. Using exec is crucial. If you don’t, the file of the first script stays open and the DVD mount point stays busy. If you use exec, the shell knows that no further line will be read from that file, closes it, and frees the DVD mount point.
3) The simplest way to pass a variable to a script is to pass it as a command line parameter. So you should invoke the second script with something like exec ./2.sh "$name" and refer to that parameter as "$1" in the text of the second script. However, if I understand correctly, what you want to pass is the same variable you have created the /tmp/name directory from, that is, the current directory of ./2.sh. If this is the case, then its value can be retrieved by name="$(basename $(pwd)) inside 2.sh.
4) export name didn’t work for you because export makes a variable known to subprocesses of the current process, but the second script was not a subprocess of the first in your scenario. export name would work if you do as I have outlined in the above point, because when you exec ./2.sh the environment stays the same, but still, it is much more readable if you pass the value explicitly.
5) In general, when you plan to do stuff based on user input, it is very important to sanitize it before doing anything: check any user-provided string for emptyness, strange characters such as spaces, newlines, slashes, semicolons, etc... check also characters which might be harmless when operating on the filesystem, but might cause problems on the newly-burned DVD, check which characters are legal at every step of your procedure... In short, don’t trust user input.

Restructuring the directories in linux/unix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have several directories structured like this in a parent directory:
/app/bpp/cpp/dpp/ASM/Report
/ghh/hhh/hhh/ASM/Report
/hh/ASM/Report
As we see above, all the ASM directories have Report directories in them along with other sub directories and files. I want a separate directory that has a parent directory to ASM (with ASM only), and ASM with Report directory in it. The result should look like this:
/dpp/ASM/Report
/hhh/ASM/Report
/hh/ASM/Report
It's not absolutely clear what you are asking; do you want to make a copy of the initial directories; do you want rather to move the initial directory to a new location? (Since you seem to want something related to "shell script" you should also tag your question with these words).
The best would probably to start with find; the following command:
find / -type d -name Report
will list all directories called Report; you could pipe the output of this command to grep in order to select those ending with /ASM/Report with:
find / -type d -name Report | grep "\/ASM\/Report$"
this would give to you a good starting point for detecting the directories to be moved/copied.
You can also use the -exec option of find for directly perform some action on a file or directory found by the command. You should type man find in order to see all the power of this tool.
It looks like you will have to search in the whole filesystem; thus find may print some warnings (related to permissions), but it shouldn't hurt; you can discard these warnings (if any) by ending the find command with 2>/dev/null for discarding the stderr stream (the error messages).

Portable bourne shell script without using functions of modern shells as bash, ksh, zsh etc [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First of all I want thank all of you who will help me solve this. I have an exam tomorrow and I have to prepare this script for the exam. I am really new to linux and those bourne shell script.
My project should be a portable bourne shell script which scans a directory for the following files: header.txt, footer.txt and content.txt. The content of the files should be read but ignoring the lines starting with # and this content should be used for generating an HTML page with the following header, footer and content. This files can contain any text and/or HTML code but the cannot contain head and body tags. When scanning the directory the script have to compare the date of the last change of the files (header.txt, footer.txt and content.txt) with the date of the last change of the HTML page (if you have one already) and if the date of the last edit on the files is newer than the one on the HTML page the script should generate a new HTML page with the latest content.
Guys thank you very much as this is very important for me. Please help me getting this done.
Thank you very much!
To remove lines beginning with # try this:
grep -v "^#" file
To remove lines that may contain spaces (or blank characters) before a #:
grep -v "^[[:blank:]]*#" file

linux commands that read specific parts of files

I'm having some troubles with homework questions for an intro linux class I am taking. Two questions in particular are really throwing me for a loop. I don't know what commands to use to list off specific parts of specific lines all in one command. This is the first time I've used linux and none of it was covered in lecture. Here are a few of the questions. I don't want the answers, just somewhere to start with the commands.
(1 command : ½ pt) You should be in your home directory.
Create a file named summer-school in your home directory that contains the file names and lines (the data, not the line numbers) in those files where students scored less than ten out of a hundred.
The files look like this:
mary;:;79/100;st407
mary;:;51/100;m395
mary;:;71/100;cs541
mary;:;75/100;ph535
mary;:;77/100;ws420
mary;:;97/100;c392
mary;:;79/100;st638
mary;:;82/100;ar523
(2 commands : 1 pt) You should be in the ~cs155/pub/hw2Files/ directory.
(Do not add this change of directory command to your list of commands)
Create a file named classes in your home directory with a list of all the classes taken by anyone.
Uses the same type of files.
Thanks for the help

Resources