<p>Anybody have a script or alias to find untracked (really: unadded) files in a Perforce tree?</p>

<p>EDIT: I updated the accepted answer on this one since it looks like P4V added support for this in the January 2009 release.</p>

[{'answer_id': 9278, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 4, 'selected': False, 'text': "<p>Ahh, one of the Perforce classics :) Yes, it really sucks that there is STILL no easy way for this built into the default commands.</p>\n\n<p>The easiest way is to run a command to find all files under your clients root, and then attempt to add them to the depot. You'll end up with a changelist of all new files and existing files are ignored.</p>\n\n<p>E.g dir /s /b /A-D | p4 -x - add</p>\n\n<p>(use 'find . -type f -print' from a nix command line).</p>\n\n<p>If you want a physical list (in the console or file) then you can pipe on the results of a diff (or add if you also want them in a changelist).</p>\n\n<p>If you're running this within P4Win you can use $r to substitute the client root of the current workspace.</p>\n"}, {'answer_id': 9280, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 6, 'selected': False, 'text': '<p>On linux, or if you have gnu-tools installed on windows:</p>\n\n<pre><code>find . -type f -print0 | xargs -0 p4 fstat &gt;/dev/null\n</code></pre>\n\n<p>This will show an error message for every unaccounted file.  If you want to capture that output:</p>\n\n<pre><code>find . -type f -print0 | xargs -0 p4 fstat &gt;/dev/null 2&gt;mylogfile\n</code></pre>\n'}, {'answer_id': 33433, 'author': 'Greg Whitfield', 'author_id': 2102, 'author_profile': 'https://Stackoverflow.com/users/2102', 'pm_score': 3, 'selected': False, 'text': '<p>Alternatively from P4Win, use the ""Local Files not in Depot" option on the left hand view panel.</p>\n\n<p>I don\'t use P4V much, but I think the equivalent is to select "Hide Local Workspace Files" in the filter dropdown of the Workspace view tab.p4 help fstat</p>\n\n<p>In P4V 2015.1 you\'ll find these options under the filter button like this:\n<a href="https://i.stack.imgur.com/nsfr5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nsfr5.png" alt="enter image description here"></a></p>\n'}, {'answer_id': 100126, 'author': 'ahu', 'author_id': 15646, 'author_profile': 'https://Stackoverflow.com/users/15646', 'pm_score': 4, 'selected': False, 'text': "<p>Under Unix:</p>\n\n<pre><code>find -type f ! -name '*~' -print0| xargs -0 p4 fstat 2&gt;&amp;1|awk '/no such file/{print $1}'\n</code></pre>\n\n<p>This will print out a list of files that are not added in your client or the Perforce depot. I've used <code>! -name '*~'</code> to exclude files ending with ~.</p>\n"}, {'answer_id': 966386, 'author': 'Ross Patterson', 'author_id': 241753, 'author_profile': 'https://Stackoverflow.com/users/241753', 'pm_score': 2, 'selected': False, 'text': '<p>I use the following in my tool that backs up any files in the workspace that differ from the repository (for Windows).  It handles some odd cases that Perforce doesn\'t like much, like embedded blanks, stars, percents, and hashmarks:</p>\n\n<pre><code>dir /S /B /A-D | sed -e "s/%/%25/g" -e "s/@/%40/g" -e "s/#/%23/g" -e "s/\\*/%2A/g" | p4 -x- have 1&gt;NUL:\n</code></pre>\n\n<p>"<strong>dir /S /B /A-D</strong>" lists all files at or below this folder (<strong>/S</strong>) in "bare" format (<strong>/B</strong>) excluding directories (/<strong>A-D</strong>).  The "<strong>sed</strong>" changes dangerous characters to their "<strong>%xx</strong>" form (a la HTML), and the "<strong>p4 have</strong>" command checks this list ("<strong>-x-</strong>") against the server discarding anything about files it actually locates in the repository ("<strong>1>NUL:</strong>").  The result is a bunch of lines like:</p>\n\n<pre><code>Z:\\No_Backup\\Workspaces\\full\\depot\\Projects\\Archerfish\\Portal\\Main\\admin\\html\\images\\nav\\navxx_background.gif - file(s) not on client.\n</code></pre>\n\n<p>Et voil&agrave;!</p>\n'}, {'answer_id': 971143, 'author': 'tenpn', 'author_id': 11801, 'author_profile': 'https://Stackoverflow.com/users/11801', 'pm_score': 7, 'selected': True, 'text': '<p><strong>EDIT:</strong> Please use <code>p4 status</code> now. There is no need for jumping through hoops anymore. See @ColonelPanic\'s <a href="https://stackoverflow.com/a/13864328/39648">answer</a>.</p>\n\n<p>In the Jan 2009 version of P4V, you can right-click on any folder in your workspace tree and click "reconcile offline work..."  </p>\n\n<p>This will do a little processing then bring up a split-tree view of files that are not checked out but have differences from the depot version, or not checked in at all.  There may even be a few other categories it brings up.  </p>\n\n<p>You can right-click on files in this view and check them out, add them, or even revert them.</p>\n\n<p>It\'s a very handy tool that\'s saved my ass a few times.</p>\n\n<p>EDIT: ah the question asked about scripts specifically, but I\'ll leave this answer here just in case.</p>\n'}, {'answer_id': 9844407, 'author': 'gdw2', 'author_id': 389812, 'author_profile': 'https://Stackoverflow.com/users/389812', 'pm_score': 2, 'selected': False, 'text': "<p>Quick 'n Dirty: In p4v right-click on the folder in question and add all files underneath it to a new changelist.  The changelist will now contain all files which are not currently part of the depot.  </p>\n"}, {'answer_id': 13864328, 'author': 'Colonel Panic', 'author_id': 284795, 'author_profile': 'https://Stackoverflow.com/users/284795', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n  <p>Is there an analogue of <code>svn status</code> or <code>git status</code>?</p>\n</blockquote>\n\n<p><strong>Yes, BUT</strong>.</p>\n\n<p>As of Perforce version 2012.1, there\'s the command <code>p4 status</code> and in P4V \'reconcile offline work\'. However, they\'re both very slow. To exclude irrelevant files you\'ll need to write a <code>p4ignore.txt</code> file per     <a href="https://stackoverflow.com/a/13126496/284795">https://stackoverflow.com/a/13126496/284795</a> </p>\n'}, {'answer_id': 18905726, 'author': 'Engineer', 'author_id': 470868, 'author_profile': 'https://Stackoverflow.com/users/470868', 'pm_score': -1, 'selected': False, 'text': '<p>In P4V, under the "View" menu item choose "Files in Folder" which brings up a new tab in the right pane.\nTo the far right of the tabs there is a little icon that brings up a window called "Files in Folder" with 2 icons. \nSelect the left icon that looks like a funnel and you will see several options. Choose "Show items not in Depot" and all the files in the folder will show up.\nThen just right-click on the file you want to add and choose "Mark for Add...". You can verify it is there in the "Pending" tab.\nJust submit as normal (Ctrl+S).</p>\n'}, {'answer_id': 33926660, 'author': 'Jac Goudsmit', 'author_id': 1365956, 'author_profile': 'https://Stackoverflow.com/users/1365956', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t have enough reputation points to comment, but Ross\' solution also lists files that are open for add. You probably do <strong>not</strong> want to use his answer to clean your workspace.</p>\n\n<p>The following uses <code>p4 fstat</code> (thanks Mark Harrison) instead of <code>p4 have</code>, and lists the files that aren\'t in the depot <strong>and</strong> aren\'t open for add.</p>\n\n<pre><code>dir /S /B /A-D | sed -e "s/%/%25/g" -e "s/@/%40/g" -e "s/#/%23/g" -e "s/\\*/%2A/g" | p4 -x- fstat 2&gt;&amp;1 | sed -n -e "s/ - no such file[(]s[)]\\.$//gp"\n</code></pre>\n\n<p>===Jac</p>\n'}, {'answer_id': 38434276, 'author': 'Rhubbarb', 'author_id': 202130, 'author_profile': 'https://Stackoverflow.com/users/202130', 'pm_score': 1, 'selected': False, 'text': '<p>The following commands produce status-like output, but none is quite equivalent to <code>svn status</code> or <code>git status</code>, providing a one-line summary of the status of each file:</p>\n\n<ul>\n<li><code>p4 status</code></li>\n<li><code>p4 opened</code></li>\n<li><code>p4 diff -ds</code></li>\n</ul>\n'}, {'answer_id': 39936860, 'author': 'Krazy Glew', 'author_id': 1051115, 'author_profile': 'https://Stackoverflow.com/users/1051115', 'pm_score': 3, 'selected': False, 'text': "<p><strong>2021-07-16: THIS ANSWER MAY BE OBSOLETE.</strong></p>\n<p>I am reasonably sure that it was accurate  in 2016, for whatever version of Perforce I was using them (which was not necessarily the most current).  But it seems that this problem or design limitation has been remedied in subsequent releases of Perforce.  I do not know what the stack overflow etiquette for this is -- should this answer be removed?</p>\n<p><strong>2016 ANSWER</strong></p>\n<p>I feel impelled to add an answer, since the accepted answer, and some of the others, have what I think is a significant problem: they do not understand the difference between a read-only query command, and a command that makes changes.</p>\n<p>I don't expect any credit for this answer, but I hope that it will help others avoid wasting time and making mistakes by following the accepted but IMHO incorrect answer.</p>\n<p><strong>---+  BRIEF</strong></p>\n<p>Probably the most convenient way to find all untracked files in a perforce workspace is <code>p4 reconcile -na</code>.</p>\n<p><code>-a</code> says &quot;give me files that are not in the repository, i.e. that should be added&quot;.</p>\n<p><code>-n</code> says &quot;make no changes&quot; - i.e. a dry-run. (Although the messages may say &quot;opened for add&quot;, mentally you must interpret that as &quot;would be opened for add if not -n&quot;)</p>\n<p>Probably the most convenient way to find all local changes made while offline - not just files that might need to be added, but also files that might need to be deleted, or which have been changed without being opened for editing via <code>p4 edit</code>, is <code>p4 reconcile -n</code>.</p>\n<p>Several answers provided scripts, often involving <code>p4 fstat</code>.  While I have not verified all of those scripts, I often use similar scripts to make up for the deficiencies of perforce commands such as <code>p4 reconcile -n</code> - e.g. often I find that I want local paths rather than Perforce depot paths or workspace paths.</p>\n<p><strong>---+ WARNING</strong></p>\n<p><code>p4 status</code> is NOT the counterpart to the status commands on other version control systems.</p>\n<p><code>p4 status</code> is NOT a read-only query. <code>p4 status</code> actually finds the same sort of changes that <code>p4 reconcile</code> does, and adds them to the repository. <code>p4 status</code> does not seem to have a <code>-n</code> dry-run option like <code>p4 reconcile</code> does.</p>\n<p>If you do <code>p4 status</code>, look at the files and think &quot;Oh, I don't need those&quot;, then you will have to <code>p4 revert</code> them if you want to continue editing in the same workspace.  Or else the changes that <code>p4 status</code> added to your changeset will be checked in the next time.</p>\n<p>There seems to be little or no reason to use <code>p4 status</code> rather than <code>p4 reconcile -n</code>, except for some details of local workspace vs depot pathname.</p>\n<p>I can only imagine that whoever chose 'status' for a non-read-only command had limited command of the English language and other version control tools.</p>\n<p><strong>---+ <code>P4V</code> GUI</strong></p>\n<p>In the GUI <code>p4v</code>, the reconcile command finds local changes that may need to be added, deleted, or opened for editing. Fortunately it does not add them to a changelist by default; but you still may want to be careful to close the reconcile window after inspecting it, if you don't want to commit the changes.</p>\n"}, {'answer_id': 44634038, 'author': 'Kevin', 'author_id': 1170023, 'author_profile': 'https://Stackoverflow.com/users/1170023', 'pm_score': 0, 'selected': False, 'text': "<p>Fast method, but little orthodox. If the codebase doesn't add new files / change view too often, you could create a local 'git' repository out of your checkout. From a clean perforce sync, git init, add and commit all files locally. Git status is fast and will show files not previously committed.</p>\n"}, {'answer_id': 46824653, 'author': 'beena', 'author_id': 8769413, 'author_profile': 'https://Stackoverflow.com/users/8769413', 'pm_score': 0, 'selected': False, 'text': '<p>The <code>p4 fstat</code> command lets you test if a file exists in the workspace, combine with <code>find</code> to locate files to check as in the following Perl example:</p>\n\n<pre><code>// throw the output of p4 fstat to a \'output file\'\n// find:\n//  -type f  :-  only look at files,\n//  -print0  :-  terminate strings with \\0s to support filenames with spaces\n// xargs:\n//  Groups its input into command lines,\n//  -0       :-  read input strings terminated with \\0s\n// p4:\n//  fstat    :-  fetch workspace stat on files\n\nmy $status=system "(find . -type f -print0 | xargs -0 p4 fstat &gt; /dev/null) &gt;&amp; $outputFile";\n\n// read output file\nopen F1, $outputFile or die "$!\\n";\n\n// iterate over all the lines in F1\nwhile (&lt;F1&gt;) {\n  // remove trailing whitespace\n  chomp $_;\n\n  // grep lines which has \'no such file\' or \'not in client\'\n  if($_ =~ m/no such file/ || $_ =~ m/not in client/){\n\n     // Remove the content after \'-\'\n     $_=~ s/-\\s.*//g;\n\n     // below line is optional. Check ur output file for more clarity.\n     $_=~ s/^.\\///g;\n\n     print "$_\\n";\n  }\n}\n\nclose F1;\n</code></pre>\n\n<p>Or you can use <code>p4 reconcile -n -m ...</code></p>\n\n<p>If it is \'opened for delete\' then it has been removed from the workspace. Note that the above command is running in preview mode (<code>-n</code>).</p>\n'}, {'answer_id': 50142337, 'author': 'givanse', 'author_id': 7852, 'author_profile': 'https://Stackoverflow.com/users/7852', 'pm_score': 0, 'selected': False, 'text': '<p>I needed something that would work in either Linux, Mac or Windows. So I wrote a Python script for it. The basic idea is to iterate through files and execute <code>p4 fstat</code> on each. (of course ignoring dependencies and tmp folders)</p>\n\n<p>You can find it here: <a href="https://gist.github.com/givanse/8c69f55f8243733702cf7bcb0e9290a9" rel="nofollow noreferrer">https://gist.github.com/givanse/8c69f55f8243733702cf7bcb0e9290a9</a></p>\n'}, {'answer_id': 64530590, 'author': 'Hazem Ramadan', 'author_id': 14519152, 'author_profile': 'https://Stackoverflow.com/users/14519152', 'pm_score': 0, 'selected': False, 'text': '<p>This command can give you a list of files that needs to be added, edited or removed:<br />\np4 status -aed ...<br />\nyou can use them separately too<br />\np4 status -a ...<br />\np4 status -e ...<br />\np4 status -d ...</p>\n'}]
['https://Stackoverflow.com/questions/9272', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1146/']